Extracting the maximum value from each array of different dimensions in a cell?

16 ビュー (過去 30 日間)
Niraj Bal Tamang
Niraj Bal Tamang 2021 年 9 月 23 日
コメント済み: Voss 2021 年 9 月 27 日
I have a cell (X) with arrays of different dimensions. I want to create a new array containing only the maximum values of each array of the cell. I tried Y=[X{:}]; but it didn't work due to different dimensions of the arrays. Can anyone please help me to exract the maximum value from each array in the cell and create a new variable out of the extracted values.

採用された回答

Voss
Voss 2021 年 9 月 23 日
Look into cellfun. You can likely do this:
X_max = cellfun(@(y)max(y(:)),X);
  2 件のコメント
Niraj Bal Tamang
Niraj Bal Tamang 2021 年 9 月 23 日
Thank you. But it didn't work. The cellfun worked when the dimensions of the array within the cell was same. Previously i tried Y=[X{:}]; which listed the elements of each array into cell columns. Then i applied the cellfun to Y. But in this case, it didn't work as the dimension was different for the arrays.
Voss
Voss 2021 年 9 月 27 日
Since each element of X is itself a cell array, you have to use cellfun twice. Try something like this:
X_max = cellfun(@(x)max(x(:)),cellfun(@(x)[x{:}],X,'UniformOutput',false),'UniformOutput',false);
Then X_max is a cell array containing the maximum value of each set of arrays forming an element of X, with empty entries where X{i} is a cell array with all empty vectors.
If you want X_max to contain some scalar value, say NaN, where X{i} is a cell array of all empty vectors, you can do something like this next:
X_max(cellfun(@(x)isempty(x),X_max)) = {NaN};
Then you can make X_max a numeric array if you want:
X_max = [X_max{:}];

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by