How do i get the output for each iteration of code?

3 visualizaciones (últimos 30 días)
Feliciano Döring
Feliciano Döring el 26 de Abr. de 2018
Comentada: Feliciano Döring el 27 de Abr. de 2018
I have a program in which i run two matrices through the same code and they give me an output for each iteration(the iteration is associated with which matrix i run through it). The thing is, in this code i run through seven iterations again(this iteration has the purpose of closing in the result i want), how do i get the last output for each matrix iteration? I mean i could remove the semicolon and check manually, but is there an easier way to do it? simply put the code looks like this
for iter=1:2
x=y{iter};
c=[0;0;0;0];
...
for k=1:7
...
c=a+b;
end
end
in which x are the different matrices i use in the code, y is the variable i use to call upon the different matrices and this c is what i'm looking for. Or even save those c's on a variable and/or a file, that would be perfect
edit. I got how to show them by using this
if k==7
disp(c)
end
but i don't know how to attach it to a variable, or cell array etc
  1 comentario
Stephen23
Stephen23 el 26 de Abr. de 2018
Editada: Stephen23 el 26 de Abr. de 2018
By using indexing. Indexing is a basic concept that is shown in the introductory tutorials:

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 26 de Abr. de 2018
Editada: Stephen23 el 26 de Abr. de 2018
By using indexing. Indexing is a basic concept that is shown in the introductory tutorials:
out = cell(1,2); % preallocate an output matrix.
for iter = 1:2
x = y{iter};
c = [0;0;0;0];
...
for k = 1:7
...
c = a+b;
end
out{iter} = c; % this will get the last value of c
end
  7 comentarios
Stephen23
Stephen23 el 27 de Abr. de 2018
Editada: Stephen23 el 27 de Abr. de 2018
"if i want to save matrices with different sizes how would i do it? Because using the cell array it doesn't seem to work"
Use a cell array. It makes no difference how large the arrays are that you put into the cells of a cell array. Here is a simple example:
>> C{1} = 1:2; % two element vector
>> C{2} = randi(100); % ten thousand element matrix
No error for me. I see no reason why it would not be possible to use a cell array. You have a bug in your code, so you should show the code that you are using. If you are getting an error then please show us the complete error message. If you are getting unexpected results then please describe in detail what you expect, and what you get.
Feliciano Döring
Feliciano Döring el 27 de Abr. de 2018
oh, i'm sorry, i actually described the problem completely wrong, the cell array works fine, it's because i tried to use cell2mat for the cell array that had two matrices with different sizes and the concatenation didn't work

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices 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