Borrar filtros
Borrar filtros

How to create a matrix from for loop result?

1 visualización (últimos 30 días)
Hang Vu
Hang Vu el 28 de Abr. de 2019
Comentada: Hang Vu el 29 de Abr. de 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

Respuesta aceptada

Jos (10584)
Jos (10584) el 28 de Abr. de 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.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by