I'm trying to create a Matrix in Matlab, which is dependent on 2 control variables $i$ and $n$. They are defined like this: $i=1:10$, $n=0:N-1$.

Now I'm trying to define a matrix, where an entry is: $cos((i/5)*pi*n)$. With every line $i$ shall increase and with every column $n$.

How can I perform this without using any loops?

Thank you very much


There are may ways, but the easiest is to take the outer product of the vectors:

cos(pi/5 * (1:10)' * (0:N-1))

Other possibilities include using meshgrid() and repmat().


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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