How can I create a nested loop ?
388 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Samantha Cepeda
il 9 Apr 2018
Commentato: Wiqas Ahmad
il 18 Mar 2021
Please, I'm trying to solve this problem: Create two “for loops”, one for i running from 1 to 5 by 1 and the other nested inside the first for j running from 1 to 8 by 1. Inside the nested loop, calculate the sum of the current I and j values and output the sum to the user followed by a tab.
I tried and did this:
clear, clc
i=0; j=0;
for i = 1:1:5 for j = 1:1:8
sum(i,j) = i + j;
end
end
fprintf ( '%g \t', sum)
I want to see if I did it right, I'm still confused on "sum" value. Thank you
3 Commenti
Risposta accettata
Von Duesenberg
il 9 Apr 2018
Here is a possible solution ; adapt it if it doesn't quite do what you had in mind :
iMax = 5;
jMax = 8;
myArray = zeros(iMax, jMax);
for iIdx = 1:iMax
for jIdx = 1:jMax
myArray(iIdx,jIdx) = iIdx + jIdx;
end
end
Più risposte (1)
muhammad mazhar
il 7 Ott 2019
if we have double sigma used for nested loop with energy formula how coulid we implment in matalab like we have an equation![Untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/241467/Untitled.png)
![Untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/241467/Untitled.png)
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!