# Understanding Index for loop

3 Ansichten (letzte 30 Tage)
Ali Tawfik am 8 Jun. 2020
Kommentiert: Ali Tawfik am 8 Jun. 2020
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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

KSSV am 8 Jun. 2020
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.
##### 2 KommentareKeine anzeigenKeine ausblenden
Ali Tawfik am 8 Jun. 2020
@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 am 8 Jun. 2020
I just uploaded a the new code !!! hope you could help !!!

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by