For Loop induced matrix manipulation , Relative index matrix

1 view (last 30 days)
>> a =[1,2,3,0,0,0,0,8,9,0,11,0,0,14,15];
So i have a big matrix and i want to make another matrix b such that it gives me the relative number index(by this what i mean is it gives me the index of representative number next to the matrix a element ) of matrix a.
i tried a for loop but didnt work to what i wanted...
for eq number 15 present in matrix a is the 8th number present(rest are zeroes) in the whole matrix a
%%%%%%%%%%%%%%% DESIRED OUTPUT %%%%%%%%%%%
[a' b']
ans =
1 1
2 2
3 3
0 0
0 0
0 0
0 0
8 4
9 5
0 0
11 6
0 0
0 0
14 7
15 8

Accepted Answer

Walter Roberson
Walter Roberson on 23 Nov 2011
[tf, b] = ismember(a, a(a ~= 0));
[a' b']

More Answers (0)

Community Treasure Hunt

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

Start Hunting!