How to save multiple output?

a =randperm(18);
b=reshape(a,9,2);
vals=b;
[m,n] =size(vals) ;
for i = 1:m
[A,x,y]=myfunction(I,vals(i,1), vals(i,2));
end
[A,x,y] is the output of myfunction.
How to save A, x and y where A is a square matrix, x and y both are single vaues.

 Respuesta aceptada

Mathieu NOE
Mathieu NOE el 15 de Sept. de 2021
hello
I would recommend to use structures (indexed) like in this example
a =randperm(18);
b=reshape(a,9,2);
vals=b;
[m,n] =size(vals) ;
I = rand(3,5);
for i = 1:m
[A,x,y]=myfunction(I,vals(i,1), vals(i,2));
output{i}.A = A;
output{i}.x = x;
output{i}.y = y;
end
% [A,x,y] is the output of myfunction.
% How to save A, x and y where A is a square matrix, x and y both are single vaues.
%%%%%%%%%%%%%%%%%%
function [A,x,y] = myfunction(I,a,b)
A = I ;
x = a;
y = b;
end
so , for example , to access the data from the first iteration :
output{1}
ans =
struct with fields:
A: [3×5 double]
x: 14
y: 7
>> output{1}.A
ans =
0.6948 0.0344 0.7655 0.4898 0.7094
0.3171 0.4387 0.7952 0.4456 0.7547
0.9502 0.3816 0.1869 0.6463 0.2760
>> output{1}.x
ans = 14

3 comentarios

Ammy
Ammy el 15 de Sept. de 2021
Thank you very much @Mathieu NOE
Mathieu NOE
Mathieu NOE el 15 de Sept. de 2021
My pleasure
I have to say that there is a better coding : rather than nesting lots of scalar structures inside a cell array, it would be simpler to just use one structure array
a =randperm(18);
b=reshape(a,9,2);
vals=b;
[m,n] =size(vals) ;
I = rand(3,5);
output = struct([]); % ensure that "output" is a structure
for ii = 1:m
[A,x,y]=myfunction(I,vals(ii,1), vals(ii,2));
[output(ii).A, output(ii).x, output(ii).y] = myfunction(I, vals(ii,1), vals(ii,2));
end
%%%%%%%%%%%%%%%%%%
function [A,x,y] = myfunction(I,a,b)
% [A,x,y] is the output of myfunction.
A = I ;
x = a;
y = b;
end
Ammy
Ammy el 15 de Sept. de 2021
well said, thank you very much.

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 15 de Sept. de 2021
In your code:
%{
for i = 1:m
[A,x,y]=myfunction(I,vals(i,1), vals(i,2));
end
%}
is A always going to be the same size? If so consider making a 3-dimensional array:
A = magic(3);
B = zeros([size(A), 5]);
for k = 1:5
B(:, :, k) = A.^k;
end
B(:, :, 3) % The cube of each element in A
ans = 3×3
512 1 216 27 125 343 64 729 8
Similarly for your scalar x and y values, store them in a vector or a 3-dimensional equivalent.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 15 de Sept. de 2021

Comentada:

el 15 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by