I need generate matrix in form like this
without using any loops in Matlab. Where N has to be some var. I need some hints or way how to solve this.
CodePudding user response:
I see two simple ways to do this. Here are some hints:
- It can be done using element-wise multiplication of a row and column vector with singleton expansion.
- Alternatively, it can be done with matrix-multiplication of a column times a row vector (in that order).
I recommend that you read the pages linked above and give it a try yourself. Here are my solutions:
- Using singleton expansion:
N = 5; C = (0:N-1).*(0:N-1).'
- Using matrix multiplication:
N = 5; C = (0:N-1).'*(0:N-1)