Back track the index of a matrix

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

 Risposta accettata

Bruno Pop-Stefanov
Bruno Pop-Stefanov il 20 Gen 2014
Modificato: Bruno Pop-Stefanov il 20 Gen 2014
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 Commenti

Hi Bruno. Thank you for the quick response. This code almost work. But the elements of my matrix A are complex numbers.
sample element set
-0.0154 -45.4596i -0.0164 -45.4599i -0.0159 -45.4598i -0.0158 -45.4597i
-0.0150 -45.4594i -0.0160 -45.4598i -0.0155 -45.4596i -0.0154 -45.4596i
-0.0136 -45.4589i -0.0147 -45.4592i -0.0142 -45.4590i -0.0141 -45.4590i
-0.0160 -45.4599i -0.0171 -45.4602i -0.0165 -45.4600i -0.0164 -45.4600i
-0.0172 -45.4604i -0.0183 -45.4607i -0.0177 -45.4605i -0.0176 -45.4605i
-0.0099 -45.4573i -0.0110 -45.4576i -0.0105 -45.4575i -0.0104 -45.4575i
-0.0058 -45.4556i -0.0069 -45.4559i -0.0063 -45.4558i -0.0062 -45.4558i
-0.0074 -45.4561i -0.0085 -45.4564i -0.0080 -45.4562i -0.0079 -45.4562i
0.0260 -45.4466i 0.0249 -45.4470i 0.0254 -45.4468i 0.0255 -45.4468i
0.0249 -45.4470i 0.0477 -45.4405i 0.0243 -45.4471i 0.0244 -45.4471i
0.0254 -45.4468i 0.0243 -45.4471i 0.0374 -45.4435i 0.0250 -45.4469i
0.0255 -45.4468i 0.0244 -45.4471i 0.0250 -45.4469i 0.0353 -45.4440i
when I used your code the answers were in the very end n elements. How can we change the code to select those elements.
You have to explain how do you want to sort your complex number, by real part? by modulus? ...
Sorry for the incomplete question.
Complex number should be sort "by modules"
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

Accedi per commentare.

Più risposte (3)

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
Andrei Bobrov
Andrei Bobrov il 21 Gen 2014
Modificato: Andrei Bobrov il 21 Gen 2014
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 il 21 Gen 2014

0 voti

Thank you all for quick responses and help. I got the answer for my assignment and learn some new codes in Matlab.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by