how to optimise the code with cell?

조회 수: 1 (최근 30일)
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2019년 2월 12일
댓글: Shubham Mohan Tatpalliwar 2019년 2월 12일
for this program i am calculating A by using for
A = cell(1,3);
for k = 1:3
A{k} = B*k + C*k;
end
And then every matrix is to be compared to get a Optimal matrix containg minimum of every every A matrix
Comparison = cat(3,A{1},A{2},A{3});
MinimumMatrix = min(Comparison./(Comparison~=0),[],3);
This is a example code
Nut in actual program i have to calculate
k=1000:10:5500
What function should be used in
Comparison = cat(3,A{1000},A{1010},.. A{5500});
to avoid writing the name of each variable for comparison matrix
  댓글 수: 2
madhan ravi
madhan ravi 2019년 2월 12일
cellfun(@(x)min(x),A,'un',0)
madhan ravi
madhan ravi 2019년 2월 12일
oops I misinterpreted please upload cell A as .mat file

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

채택된 답변

Guillaume
Guillaume 2019년 2월 12일
Why are you using cell arrays in the first place? It's just slower and has more overhead than matrices. You can just create your final matrix directly
stepcount = 3
A = zeros([size(B), stepcount]);
for k = 1:stepcount
A(:, :, k) = B*k + C*k;
end
If you really want to use an intermediate cell array, then you can use the expansion of cell arrays into comma-separated lists:
Comparison = cat(3,A{1},A{2},A{3});
%is equivalent to
Comparison = cat(3,A{:});
Comparison = cat(3,A{1000},A{1010},.. A{5500});
%is equivalent to
Comparison = cat(3,A{1000:5500});
  댓글 수: 2
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2019년 2월 12일
Actually i was using eval
but after getting some error i switched to cell....
will try stepcount now
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2019년 2월 12일
It was really helpful
thank you

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by