Logical Indexing issue

8 visualizaciones (últimos 30 días)
Jonathan
Jonathan el 7 de Oct. de 2011
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

Respuesta aceptada

Jonathan
Jonathan el 7 de Oct. de 2011
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 comentario
Fangjun Jiang
Fangjun Jiang el 8 de Oct. de 2011
You can add your acknowledgement to the comment of the answer. Please delete this entry and accept Jan's answer.

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 7 de Oct. de 2011
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)), :);

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by