Please help, how to use "for loop" to list out the coordinate/index of matrix elements in every row

  • Follow


Now i'm assigned to use >>for loop / for function<< to list the index of every max elements.

EXAMPLE:

z = randint(10,10,100);     %Z is a random generate 10 by 10 matrix
disp(z);
i=max(z,[],2);
disp(i);                             %Return the largest elements of z matrix from each row

Output:
z= 
    81    15    65    70    43    27    75    84    35     7
    90    97     3     3    38    67    25    25    83     5
    12    95    84    27    76    65    50    81    58    53
    91    48    93     4    79    16    69    24    54    77
    63    80    67     9    18    11    89    92    91    93
     9    14    75    82    48    49    95    34    28    12
    27    42    74    69    44    95    54    19    75    56
    54    91    39    31    64    34    13    25    75    46
    95    79    65    95    70    58    14    61    38     1
    96    95    17     3    75    22    25    47    56    33
 
  i=  
    84
    97
    95
    93
    93
    95
    95
    91
    95
    96
    

And now, i need to use >>for loop / for function<< to list out the coordinate/index of the max elements in every row. Lets say we take the 1st element which is "84" , its coordinate/index is i=1, j=1 .So does anyone have any idea/clue for me to use "for loop/for function to list out the coordinate of every max elements from each row?"

Thank you~
0
Reply VIC 11/30/2010 6:58:04 AM

Look at the second output argument of MAX()

Bruno
0
Reply Bruno 11/30/2010 7:05:04 AM


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <id27n0$n69$1@fred.mathworks.com>...
> Look at the second output argument of MAX()
> 
> Bruno


Thanks for your reply,

I'm not sure which one is the second output argument of MAX()

function [f,g] = patho(x)
Max = 500;
f = zeros(size(x,1),1);
g = zeros(size(x));
for k = 1:Max  %k 
   arg = sin(pi*k^2*x)/(pi*k^2);
   f = f + sum(arg,2);
   g = g + cos(pi*k^2*x);
end
?

Matlab newbie *.*
0
Reply VIC 11/30/2010 7:41:04 AM

> 
> I'm not sure which one is the second output argument of MAX()
> 

[firstoutput secondouput] = max(yourmatrix)

Bruno
0
Reply Bruno 11/30/2010 7:49:05 AM

> 
> [firstoutput secondouput] = max(yourmatrix)
> 
> Bruno

Oh~i read the description in help,
[M, Indices] = max(DMObj1) returns Indices, the indices of the maximum value(s) in DMObj1, a DataMatrix object. If DMObj1 contains a vector of data, Indices is a positive integer. If DMObj1 contains a matrix of data, Indices is a vector containing the indices for the maximum value in each column (if Dim = 1) or row (if Dim = 2). If there are multiple maximum values in a column or row, the index for the first value is returned.

z = randint(10,10,100); %Z is a random generate 10 by 10 matrix
disp(z);
t=max(z,[],2);
disp(t); %Return the largest elements of z matrix from each row

By refer to the code above,

[t,indices]=max(z);

what is the indice means? Is it a number? When i replace the indices with number (For example, [t,10]=max(z);), it showed "Invalid used of '10' on the left side of the assignment"
0
Reply VIC 11/30/2010 8:20:20 AM

Is it possible to use >>for loop<< to find the index of the max element in each row? and how to access it?
0
Reply VIC 11/30/2010 9:44:04 AM

"VIC " <vicnsc@yahoo.com> wrote in message <id2c44$5ua$1@fred.mathworks.com>...
> > 
> > [firstoutput secondouput] = max(yourmatrix)
> > 
> > Bruno
> 
> Oh~i read the description in help,
> [M, Indices] = max(DMObj1) returns Indices, the indices of the maximum value(s) in DMObj1, a DataMatrix object. If DMObj1 contains a vector of data, Indices is a positive integer. If DMObj1 contains a matrix of data, Indices is a vector containing the indices for the maximum value in each column (if Dim = 1) or row (if Dim = 2). If there are multiple maximum values in a column or row, the index for the first value is returned.
> 
> z = randint(10,10,100); %Z is a random generate 10 by 10 matrix
> disp(z);
> t=max(z,[],2);
> disp(t); %Return the largest elements of z matrix from each row
> 
> By refer to the code above,
> 
> [t,indices]=max(z);
> 
> what is the indice means? Is it a number? When i replace the indices with number (For example, [t,10]=max(z);), it showed "Invalid used of '10' on the left side of the assignment"


As Bruno stated before [firstoutput secondouput] = max(yourmatrix).  The <secondoutput> is an output, hence you don't assign it a value, the <max> function assigns it, just like it assigns the max value to <t> in your code.  

Try replacing <10> with a variable name and examining the contents.  If you are still confused, try <doc ind2sub> and <doc max>

Jason
0
Reply Jason 11/30/2010 11:41:04 AM

6 Replies
276 Views

(page loaded in 0.003 seconds)

Similiar Articles:











7/25/2012 9:35:23 PM


Reply: