how to make a loop(for...end)
Afficher commentaires plus anciens
I'm a new guy for matlab loop. Righi now I want to now some rools about loop(for...end). Such as how to make a loop to get all odd numbers from a matrix x=[1:100] or numbers like 1,5,9,13,17,21...? p.s.I know this x1=x(1:2:100) and x4=(1:4:100),but I want to know how to get it from a loop(for...end).
2 commentaires
Nahla Mohsen
le 5 Avr 2021
Write a program to find and print the value of A such that A=1+1/2+1/3+….+1/n
Réponse acceptée
Plus de réponses (3)
Paulo Silva
le 22 Mar 2011
n=[];
for a=1:2:100
n=[n a];
end
3 commentaires
Tian Lin
le 22 Mar 2011
Walter Roberson
le 22 Mar 2011
for K = 1:100
a = K:2:100;
%here, do something with the vector "a"
end
Tian Lin
le 22 Mar 2011
Walter Roberson
le 22 Mar 2011
0 votes
Paulo Silva
le 22 Mar 2011
clc
n={};
c=0;
for b=2:100
n1=[];
for a=b:2:100
n1=[n1 a];
end
c=c+1;
n{c,1}=n1;
end
The result is inside the n variable, n is a cell, each element of it contains the results n{1,1} gives you the odd numbers for 2:2:100, n{2,1} gives the odd numbers for 3:2:100 and so on...
1 commentaire
Tian Lin
le 22 Mar 2011
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!