Save multiple functions on a single vector

3 views (last 30 days)
kounoupaki87
kounoupaki87 on 25 Feb 2020
Answered: Guillaume on 25 Feb 2020
I have a function with 3 outputs
[out1,out2,out3=myfunction(dataset); (every output is a single number , it is the min avg and maximum value of the dataset)
I need to use a loop, so I will have out1,out2,out3 for every day and I want to save them in a matrix,I want every row to be a vector with the 3 outputs
Any suggestions?
  5 Comments
kounoupaki87
kounoupaki87 on 25 Feb 2020
I have a different dataset for every day (Its temperature values for Longitude and latitude points and I have them combined in a 3D matrix (lon, lat,day), now the row-column separation is not important, I just want the mim avg and max temperature values of the domain

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 25 Feb 2020
"I have a different dataset for every day (Its temperature values for Longitude and latitude points and I have them combined in a 3D matrix (lon, lat,day)"
"I will have out1,out2,out3 for every day and I want to save them in a matrix,I want every row to be a vector with the 3 outputs"
I am a bit confused then because that's what your minmaxavg function already do. However, as I said, it can be greatly simplified.
If you have R2018b or later:
dailymean = squeeze(mean(your3Dmatrix, [1 2], 'omitnan'));
dailymax = squeeze(max(your3Dmatrix, [], [1 2], 'omitnan'));
dailymin = squeeze(min(your3Dmatrix, [], [1 2], 'omitnan'));
Otherwise, if you have R2015a or later:
dailymean = mean(reshape(your3Dmatrix, [], size(your3Dmatrix, 3)), 1, 'omitnan');
dailymax = max(reshape(your3Dmatrix, [], size(your3Dmatrix, 3)), [], 1, 'omitnan');
dailymin = min(reshape(your3Dmatrix, [], size(your3Dmatrix, 3)), [], 1, 'omitnan');
And if you're still stuck on a version prior to R2015a, then you need the stats toolbox:
dailymean = nanmean(reshape(your3Dmatrix, [], size(your3Dmatrix, 3)), 1);
dailymax = nanmax(reshape(your3Dmatrix, [], size(your3Dmatrix, 3)), [], 1);
dailymin = nanmin(reshape(your3Dmatrix, [], size(your3Dmatrix, 3)), [], 1);
In any case, you don't need a loop.

More Answers (1)

Jesus Sanchez
Jesus Sanchez on 25 Feb 2020
One possibility is to work with them as vectors and then unite them in one matrix.
everyDay = 10; % N
out1 = zeros(everyDay,1); % Size Nx1
out2 = zeros(everyDay,1);
out3 = zeros(everyDay,1);
for n=1:everyDay
[out1(n),out2(n),out3(n)]=myfunction(dataset);
end
savedData = [out1(n),out2(n),out3(n)];

Community Treasure Hunt

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

Start Hunting!