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

3 ビュー (過去 30 日間)
fadams18
fadams18 2019 年 1 月 10 日
コメント済み: fadams18 2019 年 2 月 11 日
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
  1 件のコメント
Stephen23
Stephen23 2019 年 1 月 10 日
"W0_2,W0_4,W0_6,...,W0_20"
Avoid creating variables with dynamic variable names:
It is much simpler and much more efficient to use indexing.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 1 月 10 日
編集済み: Stephen23 2019 年 1 月 10 日
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 件のコメント
fadams18
fadams18 2019 年 1 月 10 日
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.
fadams18
fadams18 2019 年 2 月 11 日
Could you kindly please help me with this question? https://fr.mathworks.com/matlabcentral/answers/444284-nested-loop-plots-help
Kind regards

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by