how to do this operation on matrix ?

2 ビュー (過去 30 日間)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016 年 5 月 9 日
回答済み: Walter Roberson 2016 年 5 月 9 日
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 Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by