How to replace certain values in an array without using for-loop?

조회 수: 4 (최근 30일)
Louis
Louis 2013년 7월 16일
I have a very long array of length couple million. Say, A=[1;2;3;4;5;2;3;4;1;....] I have another array of different length (order of thousands to millions), we can call this second array B.
I would like to replace values in the first array with 0s if the value in A is present in B.
Currently, I have the following code to do it:
for i=1:length(B)
A(A==B(i))=0;
end
However, I would like to get rid of for loop and make this as efficient as possible since the arrays that I am working with is very large, and this is only a very small portion of the entire code, which needs to be computable before I graduate.
Thank you in advance,

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 16일
편집: Azzi Abdelmalek 2013년 7월 16일
A(ismember(A,B))=0
  댓글 수: 4
Muthu Annamalai
Muthu Annamalai 2013년 7월 16일
@Azzi shouldn't logical indexing work in this case?
A1( A1 == B1 ) = 0
Image Analyst
Image Analyst 2013년 7월 16일
No, it won't. That will work only if they match up. It won't remove elements that occur anywhere in A or B.

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

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by