How to reduce matrix size conditionally?

2 ビュー (過去 30 日間)
Andi
Andi 2022 年 3 月 17 日
編集済み: Davide Masiello 2022 年 3 月 17 日
Hi everyone,
My dataset consists of 194 rows and 5 column. in each row either a non-zero value or NaN. I require to pick the non-zero value (only one). There may be more than one non-zero values but all are same, so i need only one. As a resul the size of output matrix will be 194 by 1.
Thansk.

採用された回答

Voss
Voss 2022 年 3 月 17 日
編集済み: Voss 2022 年 3 月 17 日
Here's one way:
N = max(N_total,[],2);
  1 件のコメント
Andi
Andi 2022 年 3 月 17 日
Thanks

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

その他の回答 (2 件)

Simon Chan
Simon Chan 2022 年 3 月 17 日
Try this:
mean(N_total,2,'omitnan')

Davide Masiello
Davide Masiello 2022 年 3 月 17 日
編集済み: Davide Masiello 2022 年 3 月 17 日
You could use the max function. See below
% here I reproduce your type of matrix
A = rand(194,1);
A = [A,A,A,A,A];
A(logical(randi([0,1],size(A)))) = nan
A = 194×5
NaN NaN NaN 0.2306 0.2306 NaN NaN NaN 0.2761 NaN NaN NaN NaN NaN 0.3897 NaN NaN 0.5264 0.5264 0.5264 0.4432 NaN NaN 0.4432 0.4432 0.6612 NaN 0.6612 0.6612 NaN NaN NaN 0.3336 NaN 0.3336 0.3968 NaN 0.3968 0.3968 0.3968 NaN 0.7869 0.7869 0.7869 NaN 0.4621 NaN NaN NaN 0.4621
% Here I perform the required task
A = max(A,[],2)
A = 194×1
0.2306 0.2761 0.3897 0.5264 0.4432 0.6612 0.3336 0.3968 0.7869 0.4621

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by