For Loop induced matrix manipulation , Relative index matrix

1 回表示 (過去 30 日間)
karan
karan 2011 年 11 月 23 日
>> 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

採用された回答

Walter Roberson
Walter Roberson 2011 年 11 月 23 日
[tf, b] = ismember(a, a(a ~= 0));
[a' b']

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by