How to generate a logical indexing based on any element of a multiple line matrix
1 view (last 30 days)
Show older comments
Hi all,
I am struggling to produce a logical matrix based on a multiple line matrix. According to the example on the code below:
%
% A = [1048575x1];
% B = [40x1]; contains indexes to evaluate each line of A. If any element
% of A == any element of B, I would like to mark a logical YES (1). Elseif
% NO (0);
% As a result I would like to replace by "-9999" where is a logical truth
% I tried the following routine, but it is wrong:
for i=1:1:length(B)
if A == stops_start_id(i);
A = -9999;
end
end
%
Thank you for your time,
0 Comments
Accepted Answer
Guillaume
on 14 Sep 2015
A(ismember(B, A)) = -9999; %that's it, all done
The correct syntax for your loop would have been:
for ib = 1:numel(B) %prefer numel or size over length. no need to put increment when one
A(A == B(ib)) = -9999;
end
or even simpler:
for b = B' %for automatically iterates over columns, so b takes all the values of B in turn.
A(A == b) = -9999;
end
Basically, don't use if if you're using logical arrays. In any case, as I said don't use a loop.
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!