store the index of nanvalues

1 回表示 (過去 30 日間)
Tiago Dias
Tiago Dias 2018 年 3 月 2 日
コメント済み: Jos (10584) 2018 年 3 月 2 日
Hi, So i got a matrix, 5 observations for 2 variables
A = 1 2
3 4
3 NaN
Nan 5
Nan 7
I wanna store the position when the value of A is a number. so for variable 1, i got values in row 1,2 and 3. for variable 2, i got values in rows 1 2 4 5
so the result of what I want is:
Answer = 1 1
2 2
3 NaN (because A(3,2) is not a number)
NaN 4
Nan 5
or
Answer 2 = 1 1
2 2
3 4
- 5

回答 (2 件)

Jos (10584)
Jos (10584) 2018 年 3 月 2 日
編集済み: Jos (10584) 2018 年 3 月 2 日
% data
A = [ 1 2
3 4
3 NaN
NaN 5
NaN 7 ]
% engines
tf = ~isnan(A)
Answer1 = nan(size(A))
[Answer1(tf), ~] = find(tf)
Answer2 = arrayfun(@(k) find(A(:,k)), 1:size(A,2), 'un', 0)
  2 件のコメント
Tiago Dias
Tiago Dias 2018 年 3 月 2 日
what is the result of your answer2? because it's not the same of what i described in the question, since i think i would prefer answer2 over answer1 since for my main data, i have lot of variables with nans, so i would like to make a reorder, so i see the nums firstly
Jos (10584)
Jos (10584) 2018 年 3 月 2 日
Answer2 is a cell array. Each cell contains a vector with numbers. The first cell corresponds to the first column of A, and holds the rows of that column that are not NaN. Since the amount of non-NanS varies between columns, the lengths of the vectors differ between cells.

サインインしてコメントする。


KL
KL 2018 年 3 月 2 日
編集済み: KL 2018 年 3 月 2 日
Just an approach with cellarrays.
A = [1 2
3 4
3 NaN
NaN 5
NaN 7];
res = cellfun(@(x) find(~isnan(x)),num2cell(A,1),'uni',0)
result:
res{1} =
1
2
3
res{2} =
1
2
4
5
  5 件のコメント
Tiago Dias
Tiago Dias 2018 年 3 月 2 日
KL code does whata i prefer over the answer1 of Jos. thanks mate.
Tiago Dias
Tiago Dias 2018 年 3 月 2 日
編集済み: Tiago Dias 2018 年 3 月 2 日
@KL i tried to do
C = cell2table(res)
so i could get
1 1
2 2
3 4
Nan 5
but i get an error because the matrice is not consistent, could i do that in another way?, because the 1st one has 3 numbers, and the 2nd one has 4
I also tried this, but it only saves for the last j
[n,m] = size(A);
for j = 1:m
table = res{j};
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by