How to dynamically update the matrix ?

조회 수: 3 (최근 30일)
Ahsan Abbas
Ahsan Abbas 2016년 5월 8일
댓글: Ahsan Abbas 2016년 5월 8일
Hello Guys,
I have to generate matrix A=[-1 0 c1; 0 -1 c2] dynamically, for example if c1=[1 1 2 2] & c2=[3 3 4 4] are 1d arrays and if its length change after every execution of loop then the output of matrix A will increase automatically after increase in length of c1 & c2 arrays, for example in case of above example the output of A will be like...
A = [-1 0 1;
0 -1 3;
-1 0 1;
0 -1 3;
-1 0 2;
0 -1 4;
-1 0 2;
0 -1 4;]
How do i write this type of matrix dynamically ?

채택된 답변

Weird Rando
Weird Rando 2016년 5월 8일
편집: Weird Rando 2016년 5월 8일
This only works if c1 and c2 have the same length
A = [];
c1=[1 1 2 2];
c2=[3 3 4 4];
nloop = length(c1);
for ii = 1:nloop
A=[A;-1 0 c1(ii); 0 -1 c2(ii)]
end
  댓글 수: 4
Image Analyst
Image Analyst 2016년 5월 8일
There is also no need, that you've shown so far, for a cell array. A simple numerical array would be much simpler and easier.
Ahsan Abbas
Ahsan Abbas 2016년 5월 8일
Thank you Ced & Image Analyst for your responses, specially for cell2mat.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by