필터 지우기
필터 지우기

give value 0 when doesn't find element without LOOP

조회 수: 2 (최근 30일)
gabriele fadanelli
gabriele fadanelli 2020년 4월 10일
댓글: gabriele fadanelli 2020년 4월 10일
I need to find and find the row positions of elements in a matrix for every colum and return 0 if those elements are not found, e.g.:
A=[7 6 5; 9 2 3; 2 7 1]
B=[7]
sol=sum(ismember(A,B))
gives me the result
[1 1 0]
But I wish I had [1 3 0] as result since the second 7 is in the third row.
I tried also to mutiply
sol=sum(ismember(A,B))*find(ismember(A,B))
%or
sol=sum(ismember(A,B)).*find(ismember(A,B))
but none of them is correct. I hope somebody coud help me

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 4월 10일
gabriele - try multiplying the result of ismember with a row array of [1 2 3] like
>> sol = [1:size(A,2)] * ismember(A,7);
>> sol
sol =
1 3 0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by