How to find the number of non-NaN elements in a column that are NaN in the last column, in MatLab?

7 ビュー (過去 30 日間)
Let's say I have a matrix
50 15 20 35 20
NaN NaN NaN 25 20
15 20 25 NaN NaN
NaN NaN 35 20 15
I want to find out the number of elements that are non-NaN in a column but are NaN in the last column.
For example, in Column 4, there is 1 element (4,2) that is non-NaN in Column 4 but is NaN in Column 3.
How do I find this number for each column?
Thank you so much!

採用された回答

James Tursa
James Tursa 2023 年 1 月 30 日
編集済み: James Tursa 2023 年 1 月 30 日
Code is written from a slightly reordered wording:
"for each column, number of elements that are NaN in the last column and non-NaN in a column"
sum(isnan(YourMatrix(:,end)) & ~isnan(YourMatrix(:,1:end-1)))
E.g.,
YourMatrix = [
50 15 20 35 20
NaN NaN NaN 25 20
15 20 25 NaN NaN
NaN NaN 35 20 15]
YourMatrix = 4×5
50 15 20 35 20 NaN NaN NaN 25 20 15 20 25 NaN NaN NaN NaN 35 20 15
sum(isnan(YourMatrix(:,end)) & ~isnan(YourMatrix(:,1:end-1)))
ans = 1×4
1 1 1 0
Result is the number of elements matching criteria for each column except the last column.
  4 件のコメント
Walter Roberson
Walter Roberson 2023 年 1 月 30 日
YourMatrix = [
50 15 20 35 20
NaN NaN NaN 25 20
15 20 25 NaN NaN
NaN NaN 35 20 15];
sum(isnan(YourMatrix(:,1:end-1)) & ~isnan(YourMatrix(:,2:end)))
ans = 1×4
0 1 1 0
Julia
Julia 2023 年 1 月 30 日
Yes, this is exactly what I want! Your answer is such a great help for me. Thank you so much for your patience!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 1 月 30 日
isnan(YourMatrix(:, 3)) & ~isnan(YourMatrix(:, 4))

カテゴリ

Help Center および File ExchangeMatrix Computations についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by