Findd non nun values indexes in every row of a matrix

1 回表示 (過去 30 日間)
GSM
GSM 2021 年 7 月 29 日
回答済み: DGM 2021 年 7 月 29 日
Hello,
How can I find the NaN or the non-Nan indexes for every row in a matrix.
For example in the following matrix:
a =
1 1 1 NaN 1 1 1 1 1 1
2 2 2 2 NaN 2 2 2 2 2
3 3 3 3 3 NaN 3 NaN 3 3
I'd like to have the follwing answer for the NaN indexes in every row:
a1=4, a2=5, a3=[6,8]
Also, how can I store the indexes seperately for each row in case I have different number of values per row? What is the best way? A row-vector which nubmer of cells equivalent to the number of rows in the a-matrix, where each cell of the row_vectors stors the answer for each row?

採用された回答

DGM
DGM 2021 年 7 月 29 日
You could do this:
A = [1 1 1 NaN 1 1 1 1 1 1
2 2 2 2 NaN 2 2 2 2 2
3 3 3 3 3 NaN 3 NaN 3 3];
C = num2cell(isnan(A),2);
C = cellfun(@find,C,'uniform',false)
C = 3×1 cell array
{[ 4]} {[ 5]} {[6 8]}
Or depending on how you intend to use the result, you could probably just get the logical mask from isnan(A) and use its rows directly without needing to deal with find() and variable-length index vectors.

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by