logical statements of an array

1 view (last 30 days)
Rick
Rick on 15 Jun 2014
Answered: per isakson on 15 Jun 2014
Given A = [2, 4, 6, 8, 10];
I don't understand what this command is actually doing A(logical([0, 0, 1, 1, 1]))
This is what it produces
ans =
6 8 10

Answers (3)

John D'Errico
John D'Errico on 15 Jun 2014
It is equivalent to:
A(find([0 0 1 1 1]))
  1 Comment
Rick
Rick on 15 Jun 2014
could you explain in words what the command is doing? I'm a little bit stuck on that part.

Sign in to comment.


Star Strider
Star Strider on 15 Jun 2014
The command essentially works like the ‘if’ block inside the ‘for’ loop as it considers each element of logical array ‘L’ in turn:
A = [2, 4, 6, 8, 10];
L = [0 0 1 1 1]; % ‘0’ = ‘false’, ‘1’ = ‘true’
B = []; % Array ‘B’ is initially empty
for k1 = 1:length(A)
if L(k1) == 1 % If an element of ‘L’ is ‘true’
B = [B A(k1)]; % Add that element to array ‘B’
end
end

per isakson
per isakson on 15 Jun 2014

Categories

Find more on Data Type Identification 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!