Getting a number of different outputs using a single function

6 Ansichten (letzte 30 Tage)
Lewis Waswa
Lewis Waswa am 12 Mär. 2021
Kommentiert: ANKUR KUMAR am 15 Mär. 2021
Hi everyone.
I have created a function that can iteratively generate more sets of data from a smaller set of data through a random repetition. The following is the function I would like to change
% n is a multiplier/factor
% ExDataout, Erroru are outputs
% DataIn is the input
function [ExDataout,Erroru]=ExpandData(DataIn)
for m=1:100
for n=1:5
idx = sub2ind(size(DataIn), (1:size(DataIn,1))', randi(size(DataIn,2),size(DataIn,1),1));
ExDataout(:,n*m) = DataIn(idx);
Erroru=mean(ExDataout.').'-mean(DataIn.').';
end
end
end
I would like to get both the the output of the function depending on the value of n from the same function. For instance if n=1, I only have ExDataout(1) and Erroru(1). And if I have n=2, then I should have ExDataout(1), Erroru(1) as well as ExDataout(2), Erroru(2). Is there a way I could change this function to do exactly that?
Thank you.

Antworten (1)

ANKUR KUMAR
ANKUR KUMAR am 12 Mär. 2021
I have changed just the index where you are saving the output.
Focussing on "And if I have n=2, then I should have ExDataout(1), Erroru(1) as well as ExDataout(2), Erroru(2).": after each iteration of n, values are getting saved in Erroru and ExDataout.
That is why after 100 iteration of m and 5 iteration of n, you have 500 columns in the output.
First column in the output depeicts the values for m=1 and n=1.
Second column in the output depeicts the values for m=1 and n=2.
Thrid column in the output depeicts the values for m=1 and n=3.
Forth column in the output depeicts the values for m=1 and n=4.
Fifth column in the output depeicts the values for m=1 and n=5.
Sixth column in the output depeicts the values for m=2 and n=1.
Seventh column in the output depeicts the values for m=2 and n=2.
and so on...
[out1,out2]=ExpandData(randi(10,5,5))
function [ExDataout,Erroru]=ExpandData(DataIn)
for m=1:100
for n=1:5
idx = sub2ind(size(DataIn), (1:size(DataIn,1))', randi(size(DataIn,2),size(DataIn,1),1));
ExDataout(:,(m-1)*5+n) = DataIn(idx);
Erroru(:,(m-1)*5+n) =mean(ExDataout.').'-mean(DataIn.').';
end
end
end
Hope it helps.
  2 Kommentare
Lewis Waswa
Lewis Waswa am 15 Mär. 2021
Thanks @ANKUR KUMAR. This however generates the last value of n. I am still trying to fingure out how to represent the EXDataout and Erroru as cells or 3D arrays that stores these values after each iteration.
ANKUR KUMAR
ANKUR KUMAR am 15 Mär. 2021
If you wish to save in a 3D array, you can use:
[out1,out2]=ExpandData(randi(10,5,5))
function [ExDataout,Erroru]=ExpandData(DataIn)
for m=1:100
for n=1:5
idx = sub2ind(size(DataIn), (1:size(DataIn,1))', randi(size(DataIn,2),size(DataIn,1),1));
ExDataout(:,(m-1)*5+n,m) = DataIn(idx);
Erroru(:,(m-1)*5+n,m) =mean(ExDataout.').'-mean(DataIn.').';
end
end
end
If you wish to save in a cell array, you can use:
[out1,out2]=ExpandData(randi(10,5,5))
function [ExDataout,Erroru]=ExpandData(DataIn)
for m=1:100
for n=1:5
idx = sub2ind(size(DataIn), (1:size(DataIn,1))', randi(size(DataIn,2),size(DataIn,1),1));
ExDataout{m}(:,(m-1)*5+n) = DataIn(idx);
Erroru{m}(:,(m-1)*5+n) =mean(ExDataout.').'-mean(DataIn.').';
end
end
end

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by