"Double for loop", question

1 次查看(过去 30 天)
Kraka Doros
Kraka Doros 2022-5-15
a=[1 2];
b=[3 4];
n=length(a); % length(a)=length(b)=n
for i=1:n
for j=1:n
R(i)=a(j)*b(i);
end
end
% With the above R(i)=a(j)*b(i) i received 2 results in a form :
% [a(2)*b(1) , a(2)*b(2)] or [2*3 , 2*4] or [6,8].
% i can see that "b" varies following 1:n, but "a" is not
% varies and stay constand, as "n".
% Now, if i change R(i)=a(j)*b(i) to R(i)= R(i) + a(j)*b(i) :
>> R=zeros(2,1);
>> for i=1:n
for j=1:n
R(i)=R(i)+a(j)*b(i);
end
end
% i receive 2 results in a form :
% [a(1)*b(1) +a(2)*b(1) , a(1)*b(2) +a(2)*b(2)]
% or [1*3 + 2*3 , 1*4 + 2*4]
% or [3 + 6 , 4 + 8] or [9,12].
% I cannot understand, how adding R(i) in the right side of
% R(i)=a(j)*b(i), [in order to become R(i)=R(i)+a(j)*b(i)]
% make results from :
% a(2)*b(1) and a(2)*b(2)to :
% a(1)*b(1) +a(2)*b(1) and a(1)*b(2) +a (2)*b(2).
% It seems that adding R(i) in the right side, add a(1)*b(1) to the previous
% first result and a(1)*b(2) to the previous second result. "b" varies again
% following 1:n, and "a" stay constand but not as "n", like first, but as
% 1.
% Can somebody explain me, with example, how it works ?

回答(3 个)

Dyuman Joshi
Dyuman Joshi 2022-5-15
编辑:Dyuman Joshi 2022-5-15
Your data is being overwritten as the for loops runs.
For the 1st for loop
for i=1:n
for j=1:n
R(i)=a(j)*b(i);
end
end
As your j varies to 1 to n, the value of i is fixed. So the value of R(i) is overwritten as the for loop of j runs. It will store the last updated value corresponding to j i.e. n (a(n)*b(i))
That is what exactly you are obtaining - (%[a(2)*b(1) , a(2)*b(2)])
%Similarly for the 2nd for loop
R=zeros(2,1); %It should be (2,2) here
for i=1:n
for j=1:n
R(i)=R(i)+a(j)*b(i);
end
end
Here R(i) is initially 0. As the loop runs the value gets updated by the relation you have specified.
As the j for loop runs here's how your loop is modified
i=1
j=1;
R(1)=R(1)+a(1)*b(1) %which is 0 + a(1)*b(1) = a(1)*b(1)
j=2;
R(1)=R(1)+a(2)*b(1); %which is a(1)*b(1)+a(2)*b(1)
i=2
j=1;
R(2)=R(2)+a(1)*b(2) %which is 0 + a(1)*b(2) = a(1)*b(2)
j=2;
R(2)=R(2)+a(2)*b(2); %which is a(1)*b(2)+a(2)*b(2)

Kraka Doros
Kraka Doros 2022-5-16
Dyuman, Thank a lot for your detailed explanation.
Because i am totaly new in Matlab, somebody must explain every detail, for me to understand. I stop in the half of you answer (1st for loop), because if i do not fully understand it, i cannot continue to the second.
Your answer, help me a lot, but also generate more questions, as i want to learn every detail. So :
You said that as j varies from 1 to n, the value of i is fixed. But what is the rule to determine which of the j and i varies and which is fixed. I am seeing 4 cases :
% again the same a,b,n
>> a = [1 2] ;
>> b = [3 4] ;
>> n = 2 ;
% case 1
>> for i = 1 : n
for j = 1 : n
R(i) = a(j) * b(i)
end
end
R = 3 R = 6 R = 6 4 R = 6 8
% case 2
>> for i = 1 : n
for j = 1 : n
R(i) = b(j) * a(i)
end
end
R = 3 R = 4 R = 4 6 R = 4 8
% case 3
>> for i = 1 : n
for j = 1 : n
R(j) = a(j) * b(i)
end
end
R = 3 R = 3 6 R = 4 6 R = 4 8
% case 4
>> for i = 1 : n
for j = 1 : n
R(j) = b(j) * a(i)
end
end
R = 3 R = 3 4 R = 6 4 R = 6 8
Well, in the above 4 cases i have questions :
What is the role of i and j inside R() : (R(i) or R(j)). Is this, the one that determine what is fixed and what is varied (between i and j) ?
The order of a and b has a role ? I mean if it is R(j) = b(j) * a(i) or R(j) = a(i) * b(j) .
Why in the first 2 cases loop like this : one number, one number, two numbers, two numbers (R = 3 R = 4 R = 4 6 R = 4 8 and R = 3 R = 4 R = 4 6 R = 4 8, but in case 3 and 4 like this : one numb, two numb, two numb, two numb (R = 3 R = 3 6 R = 4 6 R = 4 8 and R = 3 R = 3 4 R = 6 4 R = 6 8) ?
There must be a kind of order in the loop process. I am thinging that, loop going first in the first "for", then to the second "for" and then generate the R, as it is displayed in each case. After this first result, loop reach the first "end" but do not return to the first "for". It returns to the second "for" and regenerate, again R. For the first 2 cases these 2 generations of R are both R1, that is why (i suppose), the answers begine as : one number, one number. (It overwrites it, as you allready mentioned). Then, it reach again in the first "end" and return again to the second "for" to search for n=3, and because there is no n=3, it returns to the first "for", that is now i=2, thus the new result for R is R2. Now display R1 and R2 (the 2 numbers after two first : R = 3 R = 4 R = 4 6 (1st case)) and in the last loop we get another R2 that overwrite the previous, so again we have two numbers R = 6 8 (1st case), R = 4 8 (2nd case).
With all these above, i can expalin results, but i am not sure if they are correct ?
Are they ?
Then i can go on with the 2nd loop.
  1 个评论
Dyuman Joshi
Dyuman Joshi 2022-5-17
You need to understand how nested for loops work. There are many resources available on the internet which will help you understand. I suggest looking it up on YouTube, it will be explained with help of examples.
No one here will dedicate time to teach you. We can only help you solving a problem.

请先登录,再进行评论。


Kraka Doros
Kraka Doros 2022-5-17
But you allready, in your first comment teach me how it works. My first exactly, question was "..Can somebody explain me, with example, how it works ? ". It is more than clear that is about teaching.The fact that your teaching generate more questions to me is something different. I know that nobody have much time, even me. That is why i came here to ask : To save time. Finally, following your advise to look it up but not in Youtube ; i look it up on some friend's papers i found, i saw that series of logical thoughts i made (in my previous comment) trying to explaine the results and understood (after my new-born questions) how it works (the code), were correct (if i am wrong, somebody correct me). But this took me a lot of time. It would be much simpler for me and save time, if I had a "yes your thoughts are correct", since you had already read it.
Anyway, I really appreciate your help and the time you spent explaining to me how it works despite the fact that, as you said, "no one here will dedicate time to teach you" even though the final answer (about if my series of thougths were correct), without initially expecting it, came (after spend more time) from myself.
Wish, the best for you, thanks again and
bye.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by