Back track the index of a matrix

2 ビュー (過去 30 日間)
Adam Silva
Adam Silva 2014 年 1 月 20 日
編集済み: Bruno Pop-Stefanov 2014 年 1 月 21 日
Hi everybody, I'm writing a program to output results in a matrix(n x n).Let's call it matrix "A". I need to sort all the elements in matrix A in ascending order and select the first n elements. Let's put this elements in to a vector "B".
How should I write the code to back track the original index (row and column)of the elements of B in A ?
example:
A=[ 1 3 5; 9 7 6; 2 8 4];
B= [7 4 3];
elements of B in A: row= 2 3 1
column = 2 3 2
Thank you in advanced for the attention.
Dulan

採用された回答

Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014 年 1 月 20 日
編集済み: Bruno Pop-Stefanov 2014 年 1 月 20 日
The sort function can also output the linear indices of the sorted elements. Use these indices to access the corresponding elements in A. You can transform linear indices into subscript indices using the ind2sub function.
% Input matrix
A = [ 1 3 5; 9 7 6; 2 8 4];
% Sort. Transform A into a vector with (:)
[B,IX] = sort(A(:), 'ascend');
% Convert linear indices to subscript
[I,J] = ind2sub(size(A),IX);
% Display n first elements
for i=1:3
fprintf('Element %d: %d at row %d and column %d\n', i, B(i), I(i), J(i));
end
  4 件のコメント
Adam Silva
Adam Silva 2014 年 1 月 20 日
Sorry for the incomplete question.
Complex number should be sort "by modules"
Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014 年 1 月 21 日
編集済み: Bruno Pop-Stefanov 2014 年 1 月 21 日
By default, sort orders complex numbers by modulus first, and, if two numbers have same modulus, it orders them by angle.
If you want to sort your list by angle only, then use sort on angle(A(:)) instead:
% Input matrix
A = [-0.0154-45.4596i, -0.0164-45.4599i, -0.0159-45.4598i; ...
-0.0150-45.4594i, -0.0160-45.4598i, -0.0155-45.4596i; ...
-0.0136-45.4589i, -0.0147-45.4592i, -0.0142-45.4590i];
% Sort. Transform A into a vector with (:)
[B,IX] = sort(angle(A(:)), 'ascend');
% Convert linear indices to subscript
[I,J] = ind2sub(size(A),IX);
% Display n first elements
for i=1:3
fprintf('Element %d: %d at row %d and column %d\n', i, B(i), I(i), J(i));
end

サインインしてコメントする。

その他の回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 20 日
A=[ 1 3 5; 9 7 6; 2 8 4];
B= [7 4 3];
idx=[];
idy=[];
for k=1:numel(B)
ij=ismember(A,B(k));
[ii,jj]=find(ij);
idx=[idx ii];
idy=[idy jj];
end
idx
idy
  1 件のコメント
Adam Silva
Adam Silva 2014 年 1 月 21 日
Thank you Azzi

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2014 年 1 月 21 日
編集済み: Andrei Bobrov 2014 年 1 月 21 日
A = [ 1 3 5; 9 7 6; 2 3 4];
B = [7 4 3];
[a,b] = ismember(A,B);
[r,c] = find(a);
out = accumarray(b(a),1:numel(b(a)),[],@(ii){[r(ii),c(ii)]});

Adam Silva
Adam Silva 2014 年 1 月 21 日
Thank you all for quick responses and help. I got the answer for my assignment and learn some new codes in Matlab.

カテゴリ

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