How to compact an array with repeated data and NaNs?

8 Ansichten (letzte 30 Tage)
Elin
Elin am 25 Mär. 2015
Kommentiert: Elin am 26 Mär. 2015
Hi!
I have two vectors with measurement set points and an array with measurement values. The vectors and array contain repeated values and the array also contains NaNs. I would like to remove the repeated data and for the measurement set points where there are both a NaN and a value, I would like the value to remain.
For example, if the two vectors are called x and y and the array is called z.
x = [0 0.5 0.5 1 2]
y = [0 1 2 2 ]
z = [1 NaN 2 5 10; 7 3 3 NaN 2; 8 4 4 NaN NaN; 8 NaN 4 8 NaN]
I would like this to become
x = [0 0.5 1 2]
y = [0 1 2]
z = [1 2 5 10; 7 3 NaN 2; 8 4 8 NaN]
How do I do this in an easy way (I have quite a large array with data…)?
Thanks!
  3 Kommentare
Konstantinos Sofos
Konstantinos Sofos am 25 Mär. 2015
Bearbeitet: Konstantinos Sofos am 25 Mär. 2015
And what will happen in the case that all the values of a row are NaN or the dimensions are not consistent? In your example with Z matrix the output of the last row has 2 times the 8....
Elin
Elin am 25 Mär. 2015
Bearbeitet: Elin am 26 Mär. 2015
James: That's exactly what I want to do. And, yes, you can assume that x any y are sorted.
Konstantinos: I have no measurement data where all elements in either a row or column is NaN and the dimensions are consistent, so that is not a problem. The reason the Z matrix has the value 8 two times in the last row is because both measurement set points (x = 0, y = 2) and (x = 1, y = 2) give the measurement value z = 8.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 25 Mär. 2015
Bearbeitet: James Tursa am 25 Mär. 2015
Brute force using loops (if there are different non-NaN values available for a particular spot, picks the max of them):
[m,n] = size(z);
for k=1:n-1
if( x(k) == x(k+1) )
z(:,k) = max(z(:,k:k+1),[],2);
end
end
for k=1:m-1
if( y(k) == y(k+1) )
z(k,:) = max(z(k:k+1,:),[],1);
end
end
[x,iax,~] = unique(x);
[y,iay,~] = unique(y);
z = z(iay,iax);

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by