Select the largest two numbers with their indices

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

 채택된 답변

Andrew Newell
Andrew Newell 2012년 1월 3일

0 개 추천

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

Saed
Saed 2012년 1월 3일
I will try this one, but does this retrieve the indices too. I mean if |h1|^2+|h4|^2 is the maximum, I need to know that i=1 and j=4, because later on, I will use them as h1=h(i=1,:) and h2=h(j=4,:), and use h1 and h2 as the best two random variable in the power sense.
Now I am doing some maintenance on my MATLAB so I have not tried the segment yet.
Thanks
Yes, it displays the indices.
I figure there is no need to do the sums. Just find the two largest h2 values. The largest sum is going to be formed by adding the largest values.
Oh, is that what he means by "the largest two"?
Saed
Saed 2012년 1월 4일
yes right, the largest two numbers will have the largest sum. Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 1월 3일

0 개 추천

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

Saed
Saed 2012년 1월 3일
What I meant in terms MATLAB syntax without involving too much loops.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2012년 1월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by