Hello all, i am trying to calculate a bunch of matrices using a for loop. I have written the following code
T = 60;
for t = 60:60:12940
phi(:,t) = [(n1(1,t+T)-n2(1,t+T)) / (n1(1,t)-n2(1,t)), (n3(1,t+T)-n4(1,t+T)) / (n3(2,t)-n4(2,t)); (n1(2,t+T)-n2(2,t+T)) / (n1(1,t)-n2(1,t)), (n3(2,t+T)-n4(2,t+T)) / (n3(2,t)-n4(2,t))];
end
i am getting an error code that says the left side of the equation is a 2-by-1 and the right side is a 2-by-2. both sides of the equation are supposed to be a 2-by-2!
How would i go about writing the left side of the eqution so it would work with the for loop and result in a 2-by-2?
Thanks for any help!

 채택된 답변

KSSV
KSSV 2020년 6월 18일
편집: KSSV 2020년 6월 18일

0 개 추천

T = 60;
phi = cell([],1) ;
count = 0 ;
for t = 60:60:12940
count = count+1 ;
phi{count} = [(n1(1,t+T)-n2(1,t+T)) / (n1(1,t)-n2(1,t)), (n3(1,t+T)-n4(1,t+T)) / (n3(2,t)-n4(2,t)); (n1(2,t+T)-n2(2,t+T)) / (n1(1,t)-n2(1,t)), (n3(2,t+T)-n4(2,t+T)) / (n3(2,t)-n4(2,t))];
end
If you think that always the RHS is a 2x2 matrix, ot can be saved into matrix by initlaizing it into a 3D matrix.

댓글 수: 5

hi KSSV, thank you for your reply. Could you please explain what you did there... i would have never had thought to do anything like it.
I am now getting an error message saying "Index in position 2 exceeds array bounds (must not exceed 12000)."
here is my exact code....
T = 60 % this is also 1.2 seconds
psi = cell([],1);
count = 0;
for t = 60:60:12940
count = count+1;
phi{count} = [(n1(1,t+T)-n2(1,t+T)) / (n1(1,t)-n2(1,t)), (n3(1,t+T)-n4(1,t+T)) / (n3(2,t)-n4(2,t)); (n1(2,t+T)-n2(2,t+T)) / (n1(1,t)-n2(1,t)), (n3(2,t+T)-n4(2,t+T)) / (n3(2,t)-n4(2,t))];
end
im not sure what is wrong now.
Thank you for the help!
KSSV
KSSV 2020년 6월 18일
편집: KSSV 2020년 6월 18일
What is size of n1? You are accessing more number of elements than present..To the index, you are adding 60 i.e (t+T) so you are accessing more number of elements than present.
Check the dimensions.
Initilization check it..it should be phi not psi.
Adam Levschuk
Adam Levschuk 2020년 6월 18일
n1 is a 2x12000 array. same with n2, n3, and n4. What do you think?
Thank you so much for your help,
Adam
KSSV
KSSV 2020년 6월 18일
Then you see...you are accessing 12940....it is clear....
You can access elements only upto index 12000....you need to change the loop indexing.
Adam Levschuk
Adam Levschuk 2020년 6월 18일
such a silly mistake -- thanks you so much for the help KSSV.
Very much appreciated!!!

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

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 6월 18일

1 개 추천

T = 60;
phi = cell(numel(t),1);
t = 60:60:12940;
for k = 1:numel(t)
phi{k} = [(n1(1,t(k)+T)-n2(1,t(k)+T)) / ...
(n1(1,t(k))-n2(1,t(k))), (n3(1,t(k)+T)-n4(1,t(k)+T)) / ...
(n3(2,t(k))-n4(2,t(k))); (n1(2,t(k)+T)-n2(2,t(k)+T)) / ...
(n1(1,t(k))-n2(1,t(k))), (n3(2,t(k)+T)-n4(2,t(k)+T)) / (n3(2,t(k))-n4(2,t(k)))];
end
celldisp(phi)
phi = cat(3,phi{:})

댓글 수: 3

Adam Levschuk
Adam Levschuk 2020년 6월 18일
Hello madhan, i am getting an error code taht says,
"Index in position 2 exceeds array bounds (must not exceed 12000)."
ive never undertsood this error code when making for loops. Any idea of how this would be solved??
thank you so much for your help!
Adam
madhan ravi
madhan ravi 2020년 6월 18일
편집: madhan ravi 2020년 6월 18일
I don’t have time to get into details here but to illustrate with an example:
x = rand(2,2)% has two columns
x(1,3) % but you’re trying to access the third column, which doesn’t make sense
Adam Levschuk
Adam Levschuk 2020년 6월 18일
thanks for your help Madhan, hopefully ill crack this soon!
Adam

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 6월 18일

댓글:

2020년 6월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by