how to do this operation on matrix ?

2 views (last 30 days)
Firas Al-Kharabsheh
Firas Al-Kharabsheh on 9 May 2016
Answered: Walter Roberson on 9 May 2016
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 ]

Answers (1)

Walter Roberson
Walter Roberson on 9 May 2016
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}

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!