How can I improve the speed of the following code

조회 수: 4 (최근 30일)
Hassan
Hassan 2018년 8월 17일
답변: OCDER 2018년 8월 20일
This is the .m file here.
  댓글 수: 4
Hassan
Hassan 2018년 8월 18일
This improves the code but not so much. I have uploaded the .m file and you will see in this file that I am trying to solve a problem for 100 cells and in every cell, I have either a 3 by 3 matrix of a 3 by 1 vector. In my final result for D, I obtain a 100 cells with 2 by 2 matrix which can vary in every cell. I then convert this to a vector of 200 by 2 that I use in a bigger code. Ia m just wondering if anyone has suggestion of converting the cells and doing ordinary array operations rather than cell operations.
Thanks
Jan
Jan 2018년 8월 20일
@Hassan: Your idea sounds perfect: Omit the cells, if you do not need them. Cells are required, if the stores arrays have different types are sized. Is this true in your case? "in every cell, I have either a 3 by 3 matrix of a 3 by 1 vector" is not clear.

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

답변 (1개)

OCDER
OCDER 2018년 8월 20일
I see you're using a lot of cellfun and nested for loops. By NOT using cell arrays, you could just vectorized math it seems. cellfun could be slower than normal for loops. Also, with normal for loop on cell arrays, you could convert to parfor loop. But try parfor last.
EXAMPLE:
a = rand(200);
b = rand(200);
A = num2cell(a);
B = num2cell(b);
tic
c = a.*b;
toc %0.0102 s
tic
C = cellfun(@times, A, B, 'un', 0);
toc %0.0670 s
tic
D = cell(size(A));
for j = 1:numel(A) %could be parfor j = 1:numel(A)
D{j} = A{j}*B{j};
end
toc %0.0459 s
Use cellfun for doing simple stuff like cellfun('isempty', X). Note that using the function handle "@" in cellfun is much slower.
X = num2cell(rand(200));
tic; cellfun(@isempty, X) ; toc; %0.0340 s
tic; cellfun('isempty', X); toc; %0.0005 s

카테고리

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