0
$\begingroup$

I want to implement this function $𝑓[n,m] = a^{n+m}u[n]$ using 41×41 array, where $u[n] =1$ since all the value will be positive, and $n,m$ from 0 to 40. But it gives a wrong value.

N=40
M=40
a=0.9
x =zeros(41,41)
for col = 1:41
  for row = 1:41
    for k=0:N
      for i=0:M
        x(row,col)=a.^(k+i)*1;
      end
    end
  end
end
$\endgroup$
1
  • 1
    $\begingroup$ This is not a signal processing question, but a Matlab basic programming question. There's a specific close reason that clarifies that these are off-topic here, but can be asked (if appropriate there) on Stackoverflow.com. $\endgroup$ – Marcus Müller May 8 '20 at 13:47
3
$\begingroup$

I believe this will do:

x=a.^((0:40)+(0:40)’)
$\endgroup$
2
  • $\begingroup$ +1 for the 'expanding vector' Matlab sum (weird as it may seem) $\endgroup$ – David May 8 '20 at 15:46
  • $\begingroup$ See this blog post for an interesting discussion on this «implicit expansion» behaviour: blogs.mathworks.com/loren/2016/10/24/… $\endgroup$ – Knut Inge May 16 '20 at 18:03
0
$\begingroup$
N=40;
M=40;
a=0.9;
x =zeros(M+1,N+1)
for n = 0:N
  for m = 0:M 
    % add 1 for Matlab's indexing offset
    x(n+1,m+1)=a.^(m+n)*1;
  end
end

If you want to fix your code as written try this. You have too many loops, you only need two. Knut's code is a lot faster and more efficient though

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.