Borrar filtros
Borrar filtros

Loop does not save intermediary steps

2 visualizaciones (últimos 30 días)
Radu Mihail
Radu Mihail el 31 de Dic. de 2020
Comentada: Radu Mihail el 31 de Dic. de 2020
I have a huge matrix A(1540,1540)
I am trying to get all its diagonals under the main one at 3n+1 steps in separate vectors x or strings using the following loop. They all get displayed in my command window but in the Workspace at x I get only the last one(it overwrites the intermediary steps) I am doing something silly
the code
for i = 4:3:size( A,2 )
x=diag(A,-i)'
fprintf('%d', x);
end

Respuesta aceptada

Stephen23
Stephen23 el 31 de Dic. de 2020
Editada: Stephen23 el 31 de Dic. de 2020
"Loop does not save intermediary steps"
because nothing in your code makes any attempt to save them. Because each diagonal has a different number of elements, probably the simplest storage is to use a cell array:
A = rand(1540,1540);
V = 4:3:size(A,2);
N = numel(V);
C = cell(1,N);
for k = 1:N
C{k} = diag(A,-V(k));
end
All of the diagonals will be in the cell array C:
For example, the 512th diagonal:
V(512)
ans = 1537
C{512}
ans = 3×1
0.5061 0.4939 0.9809
Basic concepts, like how to save data during a loop, are explained in the starting tutorials:
  1 comentario
Radu Mihail
Radu Mihail el 31 de Dic. de 2020
No words to thank you Stephen! Happy New Year

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by