Select the largest two numbers with their indices

2 Ansichten (letzte 30 Tage)
Saed
Saed am 3 Jan. 2012
Hi,
I have the following random variable:
h=randn(4,4)+1i.*randn(4,4);
suppose that
|hi|^2=abs(h(i,1))^2+abs(h(i,2))^2+abs(h(i,3))^2+abs(h(i,4))^2
I need to select the largest two, i.e.: |hi|^2+||hj||^2 is the maximum, and the indices i and j, in the most efficient way. How?
Thanks

Akzeptierte Antwort

Andrew Newell
Andrew Newell am 3 Jan. 2012
Here is one approach that is efficient enough:
n = 4;
h=randn(n)+1i.*randn(n);
h2 = sum(h.*conj(h),2); %sum of squares for each row
[h2sort,isort] = sort(h2,'descend');
iLargest = isort(1:2);
h2Largest = h2(iLargest);
disp(['Sum of two largest values = ',num2str(h2Largest'*h2Largest)])
(Edited in view of the discussion below.)
  5 Kommentare
Andrew Newell
Andrew Newell am 4 Jan. 2012
Oh, is that what he means by "the largest two"?
Saed
Saed am 4 Jan. 2012
yes right, the largest two numbers will have the largest sum. Thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 3 Jan. 2012
Does "the most efficient way" have to do with time to program the solution, best-case execution time, worst-case execution time, average execution time, memory consumption, algorithmic complexity, some other factor?
If you want the best execution time, then the solution could involve sending the numbers to an FPGA (which you would have to program) and retrieving the answer from it. On the other hand, due to the overhead of communicating with an FPGA, perhaps a Mex routine would be faster.
Is strict IEEE754 compliance required in the calculations?
You will be writing the code in Assembly Language, right?
  1 Kommentar
Saed
Saed am 3 Jan. 2012
What I meant in terms MATLAB syntax without involving too much loops.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by