Matlab - 安排数字

时间:2013-11-13 11:35:35

标签: arrays matlab

我有向量m,x,y和amp;我想将m1,x1,y1评论如下:

% given
m = [-4 -3 -2 2 3 4];
x = [2 5 6 7 9 1];
y = [10 23 34 54 27 32];

% required
% m1 = [2 3 4];    % only +ve value from m
% x1 = [13 14 3];  % adding numbers(in x) corres. to -ve & +ve value in m & putting below 2, 3, 4  respectively
% y1 = [88 50 42]; % adding numbers(in y) corres. to -ve & +ve value in m & putting below 2, 3, 4  respectively

m1 = m(m > 0)      % this gives me m1 as required

x1,y1的任何提示都会非常有用。

2 个答案:

答案 0 :(得分:1)

假设m构建为[vectorNegativeReversed, vectorPositiveOriginal],解决方案非常简单:

p = numel(m)/2;
m1 = m(p+1:end)
x1 = x(p+1:end) + x(p:-1:1)
y1 = y(p+1:end) + y(p:-1:1)

答案 1 :(得分:0)

一些狡猾的行动怎么样:

m = [-4 -3 -2 2 3 4];
x = [2 5 6 7 9 1];
y = [10 23 34 54 27 32];

idx = find(  (m > 0) );
xdi = find( ~(m > 0) );

m1 = m(idx)
x1 = fliplr( x(xdi) ) + x(idx)
y1 = fliplr( y(xdi) ) + y(idx)

返回:

m1 =

     2     3     4


x1 =

    13    14     3


y1 =

    88    50    42
相关问题