how to do this operation on matrix ?

2 visualizaciones (últimos 30 días)
Firas Al-Kharabsheh
Firas Al-Kharabsheh el 9 de Mayo de 2016
Respondida: Walter Roberson el 9 de Mayo de 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 ]

Respuestas (1)

Walter Roberson
Walter Roberson el 9 de Mayo de 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}

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by