Match each element of one array with each element of other array without loops

8 views (last 30 days)
Abdulllah
Abdulllah on 22 May 2019
Answered: Image Analyst on 25 May 2019
Hello,
I want to match each element of one array with elements of the other array say "cc". Then multiply with a number from the third array. I am doing using loops. The length of arrays are very large therefore it takes couple of hours. Is it possible to do wihtout loops or make it faster. Here is the code, I am doing,
uniquec=sort(unique(cc));
maxc=max(uniquec);
c35p=0.35*maxc;
g=2;
lessnum=uniquec(uniquec<=c35p);
greaternum=uniquec(uniquec>c35p);
gl=linspace(1,2,length(lessnum));
gr=linspace(2,1,length(greaternum));
newC=zeros(size(cc));
for i=1:length(gl)
newC(cc==lessnum(i))= cc(cc==lessnum(i)).*gl(i);
end
for i=1:length(gr)
newC(cc==greaternum(i))= cc(cc==greaternum(i)).*gr(i);
end
  2 Comments
Abdulllah
Abdulllah on 22 May 2019
Hello Stephen, you are right. I should not write sort. I dont have issue with the memeory, I have 64GB RAM. lessnum and greaternum are differenet numbers you have seen. I had only this logic in mind to implement. The issues are with the loops. Every entery of the loop has to comapre the full array cc.

Sign in to comment.

Answers (2)

KSSV
KSSV on 22 May 2019
Edited: KSSV on 22 May 2019
Read about ismember, ismembertol and knnsearch.
  9 Comments

Sign in to comment.


Image Analyst
Image Analyst on 25 May 2019
You say "Every entery of the loop has to comapre the full array cc."
You might want to take a look at pdist2(), if you have the stats toolbox.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by