Findd non nun values indexes in every row of a matrix

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Shifting 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