user2018
user2018

Reputation: 43

How to find indexes of columns without NaN elements of a certain row in a matrix?

I have a matrix1 like that:

matrix1 = [4    2   NaN 3
           8    4   1   3
           7    7   NaN 2
           5    NaN NaN 1];

I take from the user the row index which will be used. I need indexes of columns without NaN elements of this row. Then, these indexes should be assigned to a vector.

This is my code:

rowindex = input('Which row do you choose?: ');
vector1 = find(matrix1(rowindex,setdiff(1:end,find(isnan(matrix1(rowindex,:))))));

For example, when rowindex = 3, vector1 should be [1 2 4] but the result is [1 2 3]. What should I do to correct my code?

Upvotes: 1

Views: 45

Answers (1)

EBH
EBH

Reputation: 10440

You should write:

vector1  = find(~isnan(matrix1(rowindex,:)))

Upvotes: 2

Related Questions