How to save the result of each loop separately
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Olayinka
el 21 de Nov. de 2023
Respondida: Mathieu NOE
el 21 de Nov. de 2023
I want to create two variables which are matrices is zeros(3,2) and zeros(4,2)
y = [3 4];
for i=1:length(y)
x=zeros(y(i),2)
end
I dont want the second loop to overwrite the result of the first loop.
0 comentarios
Respuesta aceptada
Paul
el 21 de Nov. de 2023
y = [3 4];
for i=1:length(y)
x{i}=zeros(y(i),2)
end
Though not shown here, x can be preallocated based on the number of elements in y.
0 comentarios
Más respuestas (2)
Les Beckham
el 21 de Nov. de 2023
y = [3 4];
for i = 1:length(y)
x{i} = zeros(y(i), 2)
end
0 comentarios
Mathieu NOE
el 21 de Nov. de 2023
hello
some solutions :
y = [3 4];
for k=1:length(y)
% solution 1 : store as cell array
x{k} =zeros(y(k),2);
% solution 2A : store as structure
s(k).data =zeros(y(k),2);
% solution 2B : another structure
t.data{k} =zeros(y(k),2);
end
x
s
t
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!