Findd non nun values indexes in every row of a matrix

1 Ansicht (letzte 30 Tage)
GSM
GSM am 29 Jul. 2021
Beantwortet: DGM am 29 Jul. 2021
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?

Akzeptierte Antwort

DGM
DGM am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by