location of non zeros

조회 수: 2 (최근 30일)
Mate 2u
Mate 2u 2013년 7월 9일
Hi there if I have a 100x1 matrix which has some zeros in it, how can i find the corresponding matrix of "locations" for all the cells which are non-zero.
Eg, 1, 3, 2, 0 , 2, 4, 1, 0 , 0
Output:
1, 2, 3, 5, 6, 7, ......

채택된 답변

Matt J
Matt J 2013년 7월 9일
편집: Matt J 2013년 7월 9일
>> A=[ 1, 3, 2, 0 , 2, 4, 1, 0 , 0];
>> locations = find(A)
locations =
1 2 3 5 6 7

추가 답변 (1개)

Dan Seal
Dan Seal 2013년 7월 9일
Use the logical ~= (not equal). This is not an assignment, but rather a test to see where two things are not equal to each other.
>> a = [1, 3, 2, 0 , 2, 4, 1, 0 , 0]
a =
1 3 2 0 2 4 1 0 0
>> loc = a ~= 0
loc =
1 1 1 0 1 1 1 0 0

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by