matlab - Finding the largest vector inside a matrix -


i trying find largest vector inside matrix compound vectors matlab, having difficulties, thankful if me. have this:

the matrix paths (solution of dijkstra function), 1000x1000 matrix, values vectors of 1 row , different number of columns (when columns bigger 10, values appear "1x11 double, 1x12 double, etc"). matrix paths have form:

         1                   2                           3 ....   1      1                   <1x20 double>              <1x16 double>   2    <1x20 double>         2                          [2,870,183,492,641,863,611,3]   3    <1x16 double>     [3,611,863,641,492,183,870,2]   3      4    <1x25 double>         <1x12 double>               <1x14 double>   .   .   . 

at first thought in finding largest vector in matrix

b = max(length(paths)) 

however matlab returns b = 1000, value feasible, not likely. when trying find out position of vector using:

[row,column] = find(length(paths) == b) 

matlab returns row = 1, column = 1, sure wrong... have thought maybe problem of how matlab takes data. doesn't consider entries of matrix vectors, because when enter:

   length(paths(3,2)) 

it returns me 1, should return 8 understand, when introducing:

    paths(3,2) 

it returns [1x8 double] expect see whole vector. don't know do, maybe "for" loop, not know if matlab takes data of matrix vectors or simple double values.

the cell largest vector can found using cellfun , numel number of elements in each numeric matrix stored in cells of paths:

veclens = cellfun(@numel,paths); [maxlen,im] = max(veclens(:)); [rowmax,colmax] = ind2sub(size(veclens),im) 

this gets 1000x1000 numeric matrix veclens containing sizes, max gets linear index of largest element, , ind2sub translates row,column indexes.

a note on length: gives size of largest dimension. size of paths 1000x1000, length(paths) 1000. advice is, don't ever use length. use size, specifying dimension want.


if multiple vectors same length, first 1 above approach. of them (starting after max command):

maxmask = veclens==maxlen; if nnz(maxmask)>1,     [rowmax,colmax] = find(maxmask); else     [rowmax,colmax] = ind2sub(size(veclens),im) end 

or just

[rowmax,colmax] = find(veclens==maxlen); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -