フィルターのクリア

Find the last non-nan in large array

23 ビュー (過去 30 日間)
meghannmarie
meghannmarie 2019 年 5 月 30 日
コメント済み: Walter Roberson 2021 年 2 月 24 日
I have very large 4-dimensional arrays with dimensions being time,depth, lat, lon. I want to find the linear index in the depth dimension to the last non-nan data value to I can evaluate the values at the "bottom". I am looking for the output to be 3-D (time, lat, lon) with linear indexes to the last depth with a value (non-nan).
Any idea how to do this efficiently? I have a bunch of very large files to run this on.

採用された回答

Guillaume
Guillaume 2019 年 5 月 30 日
One way:
%m: a 4D matrix (time x depth x lat x lon
[~, idx] = max(cumsum(~isnan(m), 2), [], 2)
This relies on the fact that max returns the index of the first max value if several are identical. The first max value of cumsum(~isnan) is the last number before zero or more nan.
  3 件のコメント
Pepe Grillo
Pepe Grillo 2021 年 2 月 24 日
Hi, I was looking exactly the same, but when I run this code I get a 4D array and when plt is the number of values before the last NaN, but I want to plot the value,
what I am doing wrong?
[~, idx] = max(cumsum(~isnan(m), 3), [], 3) %??
Thanks
Walter Roberson
Walter Roberson 2021 年 2 月 24 日
I am not clear as to what size your m is ? You should expect the output, idx, to be the same size as m, except with the third dimension now scalar.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by