Efficiency help 2

for q = 1:length(maxtab2)
for r = 1: length(maxtab2{q})
for s = 1: length(replacewith)
if abs (maxtab2{q}(r) - replacewith(s)) ==1
maxtab2{q}(r) = replacewith(s);
end
end
end
end
maxtab2 is a list of cells, each containing 2 colums of numbers(up to 20 numbers each column).
length(maxtab2)=190 length(maxtab2{q}) would be up to 20
can anyone help me make the 3 loops more effecient please? It is taking way too long right now, Thanks!
here is the data for maxtab2, its just the content of 1 cell
{[705;4000]}
replacewith is a list of over 5000 variables. the code here chooses one of these 5000 variable and replace the corresponding maxtab2 (determined by the for loops) with the chosen variable from replacewith

댓글 수: 2

Sven
Sven 2011년 12월 7일
Andy, a couple of suggestions:
You've asked a few questions all about one program you have. The questions themselves all involve nested loops with various cells of arrays of numbers. The answers are generally ways to re-write your loops. However, you're only really providing enough information to look at your loop and try to re-write it. We never really see the full picture. It might very well be the case that the underlying data structure you have chosen means that these loops are required. If that's the case, maybe asking a bigger-picture question will help get a better answer. Try the following:
1. Upload a sample data set somewhere
2. Explain in words and code what you are trying to do *on the whole*, maybe even put in why.
I have a feeling from previous questions that some of your cells could be merged into matrices or something else in a way that would make your whole code faster AND your underlying data easier to manage.
Sean de Wolski
Sean de Wolski 2011년 12월 7일
I agree with Sven. Why?
"Because the mathematician knew it was a good idea."

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

 채택된 답변

Sean de Wolski
Sean de Wolski 2011년 12월 7일

0 개 추천

for s
if abs (maxtab2{q}(r) - replacewith(s)) ==1
maxtab2{q}(r) = replacewith(s);
end
end
I'm pretty sure this can be replaced with
maxtab{q}(r) = find(abs(maxtab{q}(r)-replacewith)==1,1,'last')
but this operation seems wierd to me. You're going to replace it with the value one less than it or one more than it, you're aware of this right?

댓글 수: 9

Andy
Andy 2011년 12월 7일
yes, because i am working on plots of a chromatogram, i need all the peaks to line up, so if they are one off, i choose the peak that is most common and put them all into that position
Andy
Andy 2011년 12월 7일
maxtab{q}(r) = find(abs(maxtab{q}(r)-replacewith)==1,1,'last')
didnt work because replacewith is not a value, it is a matrix, so from the subtraction i actually get a whole list instead of one number
Sean de Wolski
Sean de Wolski 2011년 12월 7일
If it is a matrix, then you're only running over part of it (unless it's a vector (1xn) or (nx1)).
You would have to change my above to:
maxtab{q}(r) = find(abs(maxtab{q}(r)-replacewith(1:length(replacewith)))==1,1,'last')
that's probably an oversight on my part from earlier
Andy
Andy 2011년 12월 7일
its still giving me a bit matrix for the find part, so its saying:
??? In an assignment A(:)=B, the number of elements in A and B must be the same.
Sean de Wolski
Sean de Wolski 2011년 12월 7일
I might be trying to get rid of both for-loops with that. Sorry. It would be much easier to visualize/test with sample data.
Andy
Andy 2011년 12월 7일
how do i post the data? can i email it to you or something?
Sean de Wolski
Sean de Wolski 2011년 12월 7일
A free data sharing website would be best. Or something we can copy and paste by editing your original question.
Andy
Andy 2011년 12월 7일
which data do you need?
Andy
Andy 2011년 12월 7일
i ended up using this: works well
for q=1:length(maxtab2)
for r = 1:length(maxtab2{q})
finding = abs(maxtab2{q}(r) - replacewith(1:length(replacewith)));
if ismember (1,finding)==1
maxtab2{q}(r)=replacewith(find(finding==1,1,'last'));
end
end
end

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

추가 답변 (1개)

Jerry
Jerry 2011년 12월 7일

0 개 추천

If this maxtab2 can be stored in a matrix. This whole thing can be done without iteration. But if it's a cell, I think it's pretty hard to speed it up since a lot of operations cannot be applied to cell, like "-"(minus), and "find".

댓글 수: 1

Andy
Andy 2011년 12월 7일
maxtab2 is a list of cells, and each cell contains two colums, one columns is x-axis coordinate, the other is y-axis coordinate

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

카테고리

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

태그

질문:

2011년 12월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by