MATLAB: For Loop induced matrix manipulation , Relative index matrix

indexingmatrix manipulation

>> 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

Best Answer

[tf, b] = ismember(a, a(a ~= 0));
[a' b']