문제

I have a following matrix

 aa =
  245.2708    2.6912   21.0000
  245.2778    2.3969   21.0000
  245.2847    4.9097   21.0000
  245.2917    5.5113   21.0000
  245.2986    6.9260   21.0000
  245.3056   20.5392   21.0000
  245.3403   40.6676   21.0000
  245.3472   28.8638   21.0000
  245.3542   45.3048   21.0000
  245.3611   47.9512   21.0000
  245.3681   NaN       NaN
  245.3889   15.3675   21.0000
  245.3958   15.0250   21.0000

 Time =aa(:,1);
 x    =aa(:,2);
 y    =aa(:,3);

Now I need to create a new vector 'z' and it should contain all values of 'x' when 'x' is greater than 'y' and in case 'x' is smaller than 'y' then fill 'z' with 'y'. Out put for 'z' look like as follow.

z =

  21
  21
  21
  21
  21
  21
  40.6676
  28.8638
  45.3048
  47.9512
  NaN
  21
  21
도움이 되었습니까?

해결책

z = max(x, y)

Be careful with NaN, because it is neither lower, nor greater nor equal to any number. So, if, for example, x has a NaN on a position where y doesn't, then the value in x will always be chosen.

다른 팁

If you want the output to be NaNif either value is NaN you can use

z = x+y-min(x,y);
z = zeros(size(x));
idx_x = x>y;
idx_y = x<y;
z(idx_x) = x(idx_x);
z(idx_y) = y(idx_y);

Note that this does not account for when x and y are equal, and I'm not sure you can compare a number to NaN.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top