hi, i need some help in improving this section of code. it takes over 20 seconds to run. Because i have over 150 samples to run, a 10 second improvement would mean close to 1 hour of time saved. Thanks!
for p = 1: length(unique_cell(1,:)) for f = 1:length(ions{1,p})
max_inloop(f)= (ions{2,p}(1,f))/(ions{1,p}(1,f));
max_inloop2(f)= (ions{1,p}(1,f))/(ions{2,p}(1,f));
end
max_ion_list{p} = max_inloop;
max_ion_list2{p} = max_inloop2;
end

댓글 수: 1

Alex
Alex 2012년 1월 13일
One issue you are having with speed is based off of memory, each time through the loop, MATLAb is having to re-allocate memory for a larger cell matrix. Simply defining the size of the cell before you start the loop will save a LOT of time.

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

 채택된 답변

Walter Roberson
Walter Roberson 2012년 1월 13일

0 개 추천

for p = 1 : length(unique_cell(1,:))
max_inloop = ions{2,p}(1,:) ./ ions{1,p}(1,:);
max_ion_list{p} = max_inloop;
max_ion_list2{p} = 1 ./ max_inloop;
end

추가 답변 (1개)

Alex
Alex 2012년 1월 13일

0 개 추천

This should work. I can't test it out though. Also, note the comment I made on your main post. Always pre-allocate memory if you can. Currently, within your loop, each time, Matlab has to take time to make the matrix larger.
max_ion_list = cell(length(unique_cell(1,:)), length(ions{1,p}));
max_ion_list2 = cell(length(unique_cell(1,:)), length(ions{1,p}));
f = 1:length(ions{1,p});
for p = 1: length(uniue_cell(1,:))
max_ion_list{p} = (ions{2,p}(1,f))/(ions{1,p}(1,f));
max_ion_list2{p} = (ions{1,p}(1,f))/(ions{2,p}(1,f));
end

댓글 수: 5

Andy
Andy 2012년 1월 13일
oh i did preallocate, just didnt show it, walter's code works perfect
Walter Roberson
Walter Roberson 2012년 1월 13일
Alex, with "f" being a vector, you have vector / vector, and that is going to be the mrdivide operation rather than the rdivide operation which is coded as ./
Alex
Alex 2012년 1월 20일
I was referencing max_ion_list{p} = [];
I thought, that without pre-allocation, on each loop iteration, the size of max_ion_list would grow by one, and thus the Matlab memory manager would need to allocate memory for a larger variable, copy the variable, and then clear the old memory space.
Am I wrong? Is the Matlab memory manager capable to detecting how large the variable will be at runtime?
Walter Roberson
Walter Roberson 2012년 1월 20일
In R2011b, MATLAB apparently does better on memory allocation; I do not have access to that version to experiment with.
max_ion_list{p} = []; will not help with pre-allocation, as all of max_ion_list{p} is overwritten.
Anyhow, my earlier comment to you had nothing to do with preallocation, and only had to do with the difference between the "/" operator that you coded and the "./" operator that is needed.
Alex
Alex 2012년 1월 23일
Ahh, I understand now.

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

카테고리

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

태그

질문:

2012년 1월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by