2 views (last 30 days)

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

KSSV
on 22 May 2019

Edited: KSSV
on 22 May 2019

Read about ismember, ismembertol and knnsearch.

KSSV
on 22 May 2019

Pick non-zero numbers in Locb; it gives the positions in B.

A = [5 3 4 2];

B = [2 4 4 4 6 8];

[Lia,Locb] =ismember(A,B);

A(Lia)

Locb(Locb==0) = [] ;

B(Locb)

Stephen Cobeldick
on 22 May 2019

" Locb gives number of matches of each number but it do not give indices for them. "

That is incorrect. ismember does not provide any histogram/counting functionality. Its second output are the indices of the values of A in array B. Since R2013a the default is to return the index of the first instance. In your example the first instance of the value 4 is a position 2, the first instance of value 2 is at position 1.

>> Locb(Lia)

ans =

2 1

>> B(Locb(Lia))

ans =

4 2

Sign in to comment.

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.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/463413-match-each-element-of-one-array-with-each-element-of-other-array-without-loops#comment_707574

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/463413-match-each-element-of-one-array-with-each-element-of-other-array-without-loops#comment_707574

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/463413-match-each-element-of-one-array-with-each-element-of-other-array-without-loops#comment_707579

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/463413-match-each-element-of-one-array-with-each-element-of-other-array-without-loops#comment_707579

Sign in to comment.