Effacer les filtres
Effacer les filtres

For loop for multiple vectors

2 vues (au cours des 30 derniers jours)
maria
maria le 17 Avr 2015
Modifié(e) : Star Strider le 17 Avr 2015
Hello people:
I have the following code:
L=500;
S=ones(1,L);
n=50;
S((L/2-n(i)/2):(L/2+n(i)/2))=0;
I would like to have a matrix M(10,L) where M(1,L) is S when n=50, M(2,L) is S when n=100 and M(3,L) is n=150 and so on...
Does anyone knows how to do it using a loop?
Thank you,
Maria

Réponse acceptée

Star Strider
Star Strider le 17 Avr 2015
Modifié(e) : Star Strider le 17 Avr 2015
With only three iterations, the easiest way is to use a for loop:
L=500;
S=ones(1,L);
M = zeros(10,L);
n = [50, 100, 150];
for i = 1:length(n)
S((L/2-n(i)/2):(L/2+n(i)/2))=0;
M(i,:) = S;
end
EDIT — Inserted ‘M’ preallocation.

Plus de réponses (0)

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!

Translated by