Effacer les filtres
Effacer les filtres

How to create a matrix from for loop result?

1 vue (au cours des 30 derniers jours)
Hang Vu
Hang Vu le 28 Avr 2019
Commenté : Hang Vu le 29 Avr 2019
I repelem the element by the index
D = [170, -80, -30, 0, -50, -30, 20, -60, 100, -60 -20];
Iplus=find(D>0);
for i=1:size(Iplus,2)
a=ceil(D(Iplus(i))/100);
A=repelem(Iplus(i),a*2)
end
How can I store all result as below into one matrix? like A=[1 1 1 1 7 7 9 9]
A =
1 1 1 1
A =
7 7
A =
9 9

Réponse acceptée

Jos (10584)
Jos (10584) le 28 Avr 2019
D = [170, -80, -30, 0, -50, -30, 20, -60, 100, -60 -20];
Iplus=find(D>0);
A = [] ; % initialize
for i=1:size(Iplus,2)
a=ceil(D(Iplus(i))/100);
Anew = repelem(Iplus(i),a*2)
A = [A Anew] % append
end
Note that Matlab will warn you, because A is growing every iteration. With some thinking you might be able to optimise or even vectorise this piece of code.
  1 commentaire
Hang Vu
Hang Vu le 29 Avr 2019
Thank you so much for the help!^^

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing 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