logical statements of an array

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

답변 (3개)

John D'Errico
John D'Errico 2014년 6월 15일

1 개 추천

It is equivalent to:
A(find([0 0 1 1 1]))

댓글 수: 1

Rick
Rick 2014년 6월 15일
could you explain in words what the command is doing? I'm a little bit stuck on that part.

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

Star Strider
Star Strider 2014년 6월 15일

1 개 추천

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2014년 6월 15일

답변:

2014년 6월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by