For Loop with index inside of a Matrix

Hi all, I am trying to compute this 6x6 matrix associate with index k. My code looks like this:
T=zeros(6,6);
for k=1:1:5;
S(k)=exp(2.*k.*pi.*1i./(n+1))
%T(k,:)=zeros(k,:);
T(k,:)=[-G1 G1+G2+G3+S(k)*C1 -G3-S(k)*C2 0 0 0;
0 -G3 0 0 0 0;
0 0 -G4 G4+G5+G6+S(k)*C3 -G5 0;
0 0 0 -G6 -S(k)*C4 0;
0 0 0 0 -G7 G7+S(k)*C5;
1 0 0 0 0 0];
end
The error I got is "Subscripted assignment dimension mismatch." at "T(k,:)=[-G1 G1+G2+G3+S(k)*C1 -G3-S(k)*C2 0 0 0;"
How to fix this? Where does it go wrong? Thank you for your inputs.
Frank

 Accepted Answer

It looks like you are tring to stuff a 6x6 matrix into a 1x6 vector, hence the error. Use
dbstop if error
then run your code. It will stop on that line, evaluate the LHS and the RHS and error should become obvious.

4 Comments

It does stop at the line where my matrix begins, so I am thinking my LHS isn't match the 6x6 matrix.
Than how should I write this argument? I am still new to Matlab. Thanks.
I don't know what you want, so let's talk it through and that should make it clear:
You want to calculate a 6x6 matrix for each k value - what do you want to do with this matrix? Do you want to add it to the previous? (T = T+[new_stuff])
Do you want to store it as it's own 6x6 ?
T(:,:,k) = new_stuff
Do you want to store it as a 36x1 column vector?
T = zeros(36,6);
for ii...
T(:,ii) = new_36x1
Please elaborate on the goal and it should become clear
Sorry for the confusion. I want six 6x6 matrix "T". Each matrix will have a k value.
I tried "T(:,:,k) = new_stuff". It does work but now T is a 6x6x5 matrix.
If I need to pull out a T when k=1, should I write T(:,:,1)?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!