Need help to write general syntax for matlab program for combining results of all inputs

1 view (last 30 days)
If i have a matlab program which requires input d. suppose i want to get results for d=8 and d=9. i can run program separately like- every time for d=8 and get result, then for 9 and get result.
for example for d=8, i get
A=
1 2
2 3
then again for d= 9, i get
A=
1 7
0 6
How can i modify my program to store result from previous input, so that it display result as for d= 8, d= 9 together as
*1 2
2 3*
1 7
0 6

Answers (3)

Guillaume
Guillaume on 21 Aug 2014
Edited: Guillaume on 22 Aug 2014
You could use arrayfun to stash your outputs into a cell array and then cell2mat to transform that array into a matrix of of the right shape. e.g: assuming your program (function) is called myprogram:
proginputs = [8 9]; %whatever you want them to be
progoutputs = arrayfun(@myprogram, proginputs, 'UniformOutput', false);
outputmatrix = cell2mat(progoutputs'); transposed as you want the outputs concatenated in column.
Or as a one liner:
outputmatrix = cell2mat(arrayfun(@myprogram, [8 9], 'UniformOutput', false)');

Andrew Reibold
Andrew Reibold on 21 Aug 2014
Edited: Andrew Reibold on 21 Aug 2014
Here is a sure-fire but not necessarily efficient way. This should combine the results of generating lots of 2x2 matrices that are just whatever number d is.
Replace my A with how to solve for yours, and you are good to go. :)
for d = 1:10 % Change this to however many d's you want to go through
A = d*ones(2,2); %Replace this with however you generate A
combined_results(2*d-1,1) = A(1,1);
combined_results(2*d-1,2) = A(1,2);
combined_results(2*d,1) = A(2,1);
combined_results(2*d,2) = A(2,2);
end
combined_results

Joseph Cheng
Joseph Cheng on 22 Aug 2014
just perform the standard concatenation of
Results = [];
for d=8:9
Results = [Results; your_function(d)];
end
which you stick your function results at the end of results.

Community Treasure Hunt

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

Start Hunting!

Translated by