For Loop induced matrix manipulation , Relative index matrix

1 visualización (últimos 30 días)
>> 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

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by