Insert rows in a matrix
Mostrar comentarios más antiguos
Hi, I have the following problem: matrix
AA=[NaN 1 2 3 4 5
NaN 10 20 30 40 50
...............
NaN 1E8 2E8 3E8 4E8 5E8 ];
There is possible to insert every 10 row the following row:
[NaN NaN 99 Nan 77 NaN];
without a loop?
Thank you
Respuesta aceptada
Más respuestas (1)
Anthony Poulin
el 24 de Jun. de 2015
Editada: Anthony Poulin
el 24 de Jun. de 2015
You can try something like this (with B = [NaN NaN 99 Nan 77 NaN]):
for i=10:10:100
AA = [AA(1:i, 1:end); B; AA(i+1:end,1:end)];
end
(100 is an arbitrary value)
4 comentarios
Guillaume
el 24 de Jun. de 2015
That's not going to work as you resize AA on every step of the loop, yet still use the original range of rows. On step 2, you're inserting B on row 20, which is the original row 19 of AA, on step 3, you're inserting B on row 30, which is the original row 28 of AA, etc. Each step, you're more and more off the target.
Rather than an arbitrary value, you'd use size(AA, 1) for the end index.
Ionut Anghel
el 24 de Jun. de 2015
Anthony Poulin
el 24 de Jun. de 2015
Or just replacing "i" by "i + i/10 -1".
David Verrelli
el 27 de Mzo. de 2018
Or run the loop 'backwards', as in for i=100:-10:10 (although 100 might not be the correct terminal value for your case, as already noted). Even though the matrix is still resized at each step, at least this way the subsequent changes aren't affected by previous steps.
BTW, "1:end" can just be replaced with ":" alone.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!