Getting a number of different outputs using a single function
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
ANKUR KUMAR
2021-3-12
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 个评论
ANKUR KUMAR
2021-3-15
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!