Understanding Index for loop

조회 수: 1 (최근 30일)
Ali Tawfik
Ali Tawfik 2020년 6월 8일
댓글: Ali Tawfik 2020년 6월 8일
I am new to Matlab, and I would like to understand the following index in for loop ...
I understand the indices of the values are different but they are equal in number ??
Can anyone explain to me more ??
clear all;
clc;
y=[0 .4 .8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0];
z=zeros(1,length(y));
x=[0 0.8 1.6 2.4 3.2 4.0 4.8 5.6 6.4 7.2 8];
for i=1:length(x)
for j=1:11
b(:,:,i)=x(1,i) + z(1,i); % Why both b are NOt the same... I understand the indicies are different but both are 11 ??
b_j(:,:,i)=x(1,j) + z(1,j);
end
end

답변 (1개)

KSSV
KSSV 2020년 6월 8일
Becuase the values you are saving are different......in the value b_j
If you see you have used idex i.; so only the last value of i i.e length(x) will be saved.....every time it will save the last value of b. If you replace that index with i, then both will be same.
y=[0 .4 .8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0];
z=zeros(1,length(y));
x=[0 0.8 1.6 2.4 3.2 4.0 4.8 5.6 6.4 7.2 8];
for i=1:length(x)
for j=1:11
b(:,:,i)=x(1,i) + z(1,i); % Why both b are NOt the same... I understand the indicies are different but both are 11 ??
b_j(:,:,j)=x(1,j) + z(1,j);
end
end
isequal(b,b_j)
Any ways, you are calculating b_j repeatedly ..I don't know what you are trying to understand, but I think you got it what I meant to say.
NOTE: Read about initilaization. Read about indexing.
  댓글 수: 2
Ali Tawfik
Ali Tawfik 2020년 6월 8일
@KSSV Thanks, I am not sure I understood what you said...
The above question is to education purposes ... because I do believe the index are the same values (both are eleven ... I mean the size of the b_j should be (1,1,11) whatever it's i or j the important I guess is the number i.e b_j(:,:,d) whatever the name of d should be 11 ....why only the last number in x saved not for each iteration (which are 11) ...
thanks and wish you get my point ...
The question since yesterday... I already reorganized the code....
and wish you could help me...
I want the A_t1 to be equal to A_t2... I understood what you had mentioned yesterday but now... wondering how they are not the same ...
Ali Tawfik
Ali Tawfik 2020년 6월 8일
I just uploaded a the new code !!! hope you could help !!!

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by