English 中文(简体)
如何让MATLAB在2D阵列显示最低值指数?
原标题:How to get MATLAB to display the index of the minimum value in a 2D array?
  • 时间:2011-02-22 10:08:05
  •  标签:
  • matlab

I m试图在MATLAB中写一个字,找到2D阵列最低值的位置。 我确信,这一阵列中只有1个最低点,因此,在阵列中具有相同最低价值的多个地点不是一个问题。 我可以找到阵列的最低,但在30x30阵列中,我想知道最低值是哪一行和一栏。

最佳回答

作为一种替代版本,如果你已经计算了当时刚刚使用的最低限度标准,就把min合起来,以获得最低值,并设法退回指数。

>> a=magic(30);
>> [r,c]=find(a==min(min(a)))

r =
     1
c =
     8

或者取决于你如何利用你可能希望用一个逻辑阵列来界定的地点信息,在这种情况下,合乎逻辑的解决办法可以用来给你一个真相表。

>> a=magic(30);
>> locn=(a==min(min(a)));
问题回答

您可以将矩阵改成病媒,利用MIN找到最低指数,然后将这一线性指数转化为矩阵指数:

>> x = randi(5, 5)

x =

     5     4     4     2     4
     4     2     4     5     5
     3     1     3     4     3
     3     4     2     5     1
     2     4     5     3     5

>> [value, index] = min(reshape(x, numel(x), 1));
>> [i,j] = ind2sub(size(x), index)

i =

     3


j =

     2

参看min功能。 它可以退还最低值和指数。 就两个层面而言,仅将其称为两倍。

A = rand(30); % some matrix
[minColVal, minColIdx] = min(A);
[minRowVal, minRowIdx] = min(minColVal);

minVal = minRowVal;
minValIdx = [minColIdx(minRowIdx), minRowIdx];

Edit: @b3 解决办法在计算上可能比较少临时空间的用户要多。

To find min or max in a subset of a vector - If A is a vector and "lowerBound" and "upperBound" are the bounds of the vector among which you need to find the max (or min) value, then use this command -

[Value,Index]=min(A(lowerBound:upperBound));

This returns "Value" as the min or max value among A(lowerBound) and A(uppedBound) and "Index" as with "lowerBound" as the offset. So to find the absolute index, you need to add "lowerBound" to the Index.

使用在线功能的另一种解决办法将发挥作用。

    >> min_index = @(matrix) find(matrix == min(reshape(matrix, [1,numel(matrix)])));

    >> a=magic(30);
    >> [r,c]=min_index(a)

    r =
         1

    c =
         8




相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...

热门标签