How to save inner loop data with outer loop data in one matfile

Hello Matlabers!
So basically i have this code
for i=1:5
r=5;
W = rand(m,r);
H = rand(r,n);
for rank=2:2:20 % Now for every value of rank I want to compute W0 and H0
W0 = rand(m,rank);
H0 = rand(rank,n);
end
% finally in the end I want to save all values. For each iteration of the OUTER loop,
% I will have all values of the INNER loop as well.
save( ['DATA/data_',int2str(i),'.mat'],'W','H','W0','H0','-v7.3' )
end
So i wish to have my matfile contain files like
Data_1
W,H
W0_2,W0_4,W0_6,...,W0_20
Data_2
W,H
W0_2,W0_4,W0_6,...,W0_20
.
.
Data_5
W,H
W0_2,W0_4,W0_6,...,W0_20

 채택된 답변

Stephen23
Stephen23 2019년 1월 10일
편집: Stephen23 2019년 1월 10일

1 개 추천

Using a structure (or any other array) is better than using dynamically named variables:
m = 2;
n = 3;
r = 5;
V = 2:2:20;
for ii = 1:5
W = rand(m,r);
H = rand(r,n);
S = struct();
for jj = 1:numel(V)
S(jj).W0 = rand(m,V(jj));
S(jj).H0 = rand(V(jj),n);
end
F = fullfile('DATA',sprintf('data_%d.mat',ii));
save(F,'W','H','S','V')
end

댓글 수: 7

Hello Thanks, I would like the W0 and H0 to be saved like the W and H and not into a structure. becaue i would want to load the mat files and use W0 and H0 as an input to a function later. is this possibe?
Stephen23
Stephen23 2019년 1월 10일
편집: Stephen23 2019년 1월 10일
@fadams18: then use a cell array, or an ND array. Anything with indexing will be simple and efficient. I strongly recommend against forcing pseudo-indices or meta-data into variable names.
fadams18
fadams18 2019년 1월 10일
편집: fadams18 2019년 1월 10일
@Stephen Please help a brother out :D. Im zero in matlab. How do I do that with a cell array or the ND array you suggest.
Stephen23
Stephen23 2019년 1월 10일
편집: Stephen23 2019년 1월 10일
m = 2;
n = 3;
r = 5;
V = 2:2:20;
for ii = 1:5
W = rand(m,r);
H = rand(r,n);
W0 = cell(1,numel(V));
H0 = cell(1,numel(V));
for jj = 1:numel(V)
W0{jj} = rand(m,V(jj));
H0{jj} = rand(V(jj),n);
end
F = fullfile('DATA',sprintf('data_%d.mat',ii));
save(F,'W','H','W0','H0','V')
end
Thank you so much.
I Just appllied your idea. I am trying to load the files and you use them as input to my function NMF
function [] = runs(sNR,Mv,i,Tmax,passes)
matcontent = fullfile('DATA', sprintf('data_%d_%d_%d.mat',sNR,Mv,i));
load(matcontent,'Q','X','r','rank','W','H','W0', 'H0','V');
for combidx = 1:numel(W0) %try every combination
% index the cell arrays:
[~,~,RRE_NMF,T_NMF]=NMF(X,Q,matcontent.Winit{combidx}, matcontent.Hinit{combidx},Tmax,passes);
save( ['output/NMF_',int2str(sNR),'_',int2str(100*Mv),'_',int2str(rank),'_',int2str(i),'.mat'], 'RRE_NMF', 'T_NMF', '-v7.3' );
end
end
When i run it, i get this error
Dot indexing is not supported for variables of this type.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2019년 1월 10일

댓글:

2019년 2월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by