Reputation: 171
I am trying to find the entry of matrix A
that has the maximum value.
I have generated matrix A
, how can I ask MATLAB to return the four indices in addition to the maximum value of the entry within matrix A
for i = 1:size(CB,2)
for j=1:size(CB,2)
for k=1:size(CB,2)
for l=1:size(CB,2)
A(i,j,k,l)= (abs( conj(transpose([CB(:,i); CB(:,j)]))*MATRIX* [CB(:,k); CB(:,l)])^2);
end
end
end
end
Upvotes: 0
Views: 146
Reputation: 391
Use 1-D indexing:
[M,I] = max(A(:));
I
is then the index in A
where M
resides (i.e., M = A(I)
)
Then you need to use the following to convert from 1D indexing to 4D indexing:
[a,b,c,d] = ind2sub(size(A),I);
Upvotes: 4
Reputation: 12214
You can use a combination of max
and ind2sub
:
a = rand(5, 5, 5, 5);
[maxa, maxidx] = max(a(:));
[I, J, K, L] = ind2sub(size(a), maxidx);
Which we can test:
>> a(I, J, K, L) == maxa
ans =
1
The way this works is that we receive a linear index from the second output of the max
command. I used the colon operator with max
so our input is really one long column vector of a
, and the output is the maximum value of the entire matrix, maxa
, along with the location of that value in the column vector, maxidx
. You can then use ind2sub
with size
to convert that linear index into subscripts for your matrix.
Upvotes: 5