How to construct a loop that changes matrix dimensions?

6 次查看(过去 30 天)
So, I've created a 10x10 matrix using a for loop. But now, using the same formula for each component that I had inside the loop, I need to make another loop that calculates new matrices with other 9 dimensions (10x100, 100x100, 1000x10, and so on.) I had no idea how to do this first loop withoit initializing the matrix with a known size, so now I'm stuck as to how to keep going. Any help/tips would be appreciated, I'm very new to MATLAB
  2 个评论
Matt J
Matt J 2021-1-27
I had no idea how to do this first loop withoit initializing the matrix with a known size
You should do this all the time. Maybe if you post your code, we could see why you think doing so creates difficulties..
Lara Miranda
Lara Miranda 2021-1-27
I have first M=zeros(10,10);
Then, I made a for loop saying for i=1:10, for j=1:10, and inside them added the equation (e.g M(i,j)= i/j)
Now I'm wondering what I could do to create a loop that changes the dimensions such that I get 9 different sized matrices changing the value of i and j each time.

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2021-1-28
For example,
I={2,4,6};
J={3,5,7};
Matrices=cell(3,3);
for m=1:numel(I)
for n=1:numel(J)
Matrices{m,n}=(1:I{m})./(1:J{n}).';
end
end
Matrices
Matrices = 3x3 cell array
{3×2 double} {5×2 double} {7×2 double} {3×4 double} {5×4 double} {7×4 double} {3×6 double} {5×6 double} {7×6 double}

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by