how will use nested loop
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hello my problem is that i have 11 value for any variable suppose i=1:11 and have another variable suppose b=2:12 also has 11 values now my question is that i want to find out value for i=1 for z=i*b total 11 values and for i=2 another 11 value of z and so on how i can use nested loop
3 commentaires
Réponse acceptée
Muruganandham Subramanian
le 19 Déc 2012
Modifié(e) : Muruganandham Subramanian
le 19 Déc 2012
try this:
z = zeros(11,11);
for a=1:11
for b=2:12
z(a,b)=a*b;
end
end
z(:,1)=[];
2 commentaires
Walter Roberson
le 19 Déc 2012
You declare z as 11 x 11, but then you use it as 11 x 12.
Because of the way MATLAB handles assignments to non-existant locations, this will work, but it does indicate a logic fault on your part.
Muruganandham Subramanian
le 19 Déc 2012
Modifié(e) : Muruganandham Subramanian
le 19 Déc 2012
@walter..I agre your point..
Plus de réponses (3)
Babak
le 18 Déc 2012
Z = zeors(11,11);
for i=1:11
for b=2:12
z=i*b;
end
end
4 commentaires
Image Analyst
le 19 Déc 2012
If the number you're multiplying by is a fractional number, like 0.1 or 0.11 then you'll have to separate your index from your number, like I think you originally had in your message where you had a and b instead of i and b. You could just make the loop index from 1 to 11 and then create the array index from it like arrayIndex = i / 10 and then use arrayIndex in z() or wherever.
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!