Logical Indexing issue

Hi,
I use Matlab mostly to handle and analyze large environmental datasets. Recently I've been trying to reduce matrices with NaNs in them to a subset. This would be easy if a given row, matrix(i,:), was all NaNs, but only one of the elements in the third column of that row is a NaN. Given that, I'd like to reject that row, and establish a second matrix without that/those row(s). I'm trying the following at the moment:
reject=zeros(length(matrix1.(:,1)),1);
len=length(matrix1(:,1));
for j=1:len
reject(j,1)=isfinite(matrix1(j,3));
end
reject3=logical(horzcat(reject, reject, reject));
matrix2=matrix1(reject3);
In theory, this should scan the third column of matrix1 (where NaNs are possible), and establishe a logical of that column, then make a [lenx3] logical of that which matches matrix1 in dimensions. Matrix2, then, should be the reduced set. But when I do this, matrix2 is returned to me as a vector of the three columns combined.
Can you suggest a way to go about this, or a quick fix to my code that might make this possible? Feel free to point out any other problems in the code. I'm a musician-turned-inefficient Matlab user... I have a feeling this could be achieved in four simple lines if I knew what I was doing.
TIA, Jonathan

 採用された回答

Jonathan
Jonathan 2011 年 10 月 7 日

0 投票

Ah, perfect! I knew someone out there would know how to do that simply (or could think it up on the fly). Thanks very much, Jan. That worked brilliantly.
Jonathan

1 件のコメント

Fangjun Jiang
Fangjun Jiang 2011 年 10 月 8 日
You can add your acknowledgement to the comment of the answer. Please delete this entry and accept Jan's answer.

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

その他の回答 (1 件)

Jan
Jan 2011 年 10 月 7 日

1 投票

I'm not sure, but perhaps you want this:
index = isfinite(matrix1(:, 3));
matrix2 = reshape(matrix1(cat(2, index, index, index)), [], 3);
Easier:
matrix2 = matrix1(isfinite(matrix1(:, 3)), :);

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by