Azzera filtri
Azzera filtri

how to do this operation on matrix ?

2 visualizzazioni (ultimi 30 giorni)
Firas Al-Kharabsheh
Firas Al-Kharabsheh il 9 Mag 2016
Risposto: Walter Roberson il 9 Mag 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 ]

Risposte (1)

Walter Roberson
Walter Roberson il 9 Mag 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}

Community Treasure Hunt

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

Start Hunting!

Translated by