필터 지우기
필터 지우기

How to replace use of cell storage with use of arrays?

조회 수: 1 (최근 30일)
POLLY
POLLY 2018년 9월 24일
댓글: Stephen23 2018년 9월 27일
Thanks for taking the time to read this. This is a part of the script I'm working on:
resultX0=cell(length(n),length(q), ntrials);
resultY0=cell(length(n),length(q), ntrials);
resultG=cell(length(n),length(q), ntrials);
for i=1:length(n)
for j=1:length(q)
for k=1:1:ntrials
[G,X0,Y0]=matrix_plantsubm(M,N,c,n(i),p,q(j));
resultX0{i,j,k}=X0;
resultY0{i,j,k}=Y0;
resultG{i,j,k}=G;
end
end
end
I would like to replace use of cell storage with use of arrays (e.g. resultG{i,j,k} = G). I want to run the function 10 times (in this case ntrials) for each value q and n. How can I do that?
Thanks
  댓글 수: 5
POLLY
POLLY 2018년 9월 24일
They are the matrices 500*500 that were randomly generated for each i,j,k
James Tursa
James Tursa 2018년 9월 24일
What code do you currently use to save and access the cells?

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

채택된 답변

Nicole Peltier
Nicole Peltier 2018년 9월 24일
편집: Nicole Peltier 2018년 9월 24일
According to a previous time you posted about matrix_plantsubm.m on Matlab Answers (thanks Google!), it looks like G, X0, and Y0 are all MxN matrices. Therefore, you could declare resultX0, resultY0, and resultG as follows:
resultX0 = zeros(length(n), length(q), ntrials, M, N);
resultY0 = zeros(length(n), length(q), ntrials, M, N);
resultG = zeros(length(n), length(q), ntrials, M, N);
Then your for-loops should look like this:
for i=1:length(n)
for j=1:length(q)
for k=1:ntrials
[G,X0,Y0]=matrix_plantsubm(M,N,c,n(i),p,q(j));
resultX0(i,j,k,:,:)=X0;
resultY0(i,j,k,:,:)=Y0;
resultG(i,j,k,:,:)=G;
end
end
end
Hope this helps!
  댓글 수: 5
Nicole Peltier
Nicole Peltier 2018년 9월 26일
Is there a reason why this loop must be separate from the previous one? If not, I'd recommend combining the loops like this:
% Declare all variables here
resultX0 = zeros(length(n), length(q), ntrials, M, N);
resultY0 = zeros(length(n), length(q), ntrials, M, N);
resultG = zeros(length(n), length(q), ntrials, M, N);
resultX=zeros(length(n),length(q), ntrials, M, N);
resultY=zeros(length(n),length(q), ntrials, M, N);
for i=1:length(n)
for j=1:length(q)
gamma = 6/((q(j) - p)*n(i));
for k=1:ntrials
% The following code is from the first loop
[G,X0,Y0]=matrix_plantsubm(M,N,c,n(i),p,q(j));
resultX0(i,j,k,:,:)=X0;
resultY0(i,j,k,:,:)=Y0;
resultG(i,j,k,:,:)=G;
% Consolidate code from second loop into first one
[X,Y,Q, iter] = ADMM(G, c, n(i), gamma, tau, opt_tol, verbose);
resultX(i,j,k, :, :)=X;
resultY(i,j,k, :, :)=Y;
end
end
end
This way, you can use the variable G which you've already calculated instead of having to select the correct index out of resultG. (Otherwise you'll have to use resultG(i,j,k,:,:) as your input argument to ADMM.)
Stephen23
Stephen23 2018년 9월 27일
You might also like to consider ordering the dimensions slightly differently, to keep the matrices as the first two dimensions:
(M, N, length(n), length(q), ntrials)
That might make processing/accessing the data easier later.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by