how to do this operation on matrix ?

2 次查看(过去 30 天)
i want a function to do this
matrix = [{1 0 1 ; { 1 1 0 ; { 0 1 0
0 1 1 0 0 1 1 1 0
1 1 1} 0 0 0 } 0 1 1 } ]
and use this function to calculate the sum of ones in each matrix like this
Val = [ 7 , 3 , 5]
then i want to sort Val like this
val = [ 3 , 5 ,7]
after that i want to select the matrix which have the value 3
  • note that the matrix will be generate randomly so use the matrix {2} is not acceptable
i want when i call min(val) the function get back the
[ 1 1 0
0 0 1
0 0 0 ]

回答(1 个)

Walter Roberson
Walter Roberson 2016-5-9
Val = cellfun(@nnz, matrix);
[val, valorder] = sort(Val);
matrix(valorder(val == 3))
To have min(val) return an element of matrix, you are going to have to write your own object oriented class with rather odd properties.
By the way, have you considered,
Val = cellfun(@nnz, matrix);
[~, idx] = min(Val);
matrix{idx}

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by