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 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