Findd non nun values indexes in every row of a matrix

1 view (last 30 days)
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?

Accepted Answer

DGM
DGM on 29 Jul 2021
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.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!