如何在MATLAB中移动数组元素(列向量)?

时间:2018-07-21 06:34:00

标签: arrays matlab for-loop matrix vector

我有两个数组(列向量)。 a1 = [2; 3; 5; 7; 6]; 显示(a1) a2 = [9; 4; 2; 3; 5]; disp(a2)。 我想上下移动a2的元素。这是我的代码:

a1 = [2;3;5;7;6];     % column vector 1
disp(a1)              % column vector 1
a2 = [9;4;2;3;5];     % column vector 2
disp(a2)              % column vector 2
d1 = a1-a2;           % subtracting elements of a1 with a2 **(SHIFT 0)**
d1 = abs(d1);         % for magnitude only
disp(d1)              % display d1 magnitude
sz1 = size(d1);       % get the size of d1
nRows = sz1(1);       % get total number of rows in d1
disp(nRows)           % display total number of rows in d1
s1 = 0;               % intializing s1 for addition purpose
for i = 1:nRows       % this for loop is adding all elements of d1
    s1 = s1 + d1(i);
end
disp(s1)              % display s1 after adding all elements of d1

j = 1;                % **(SHIFT 1 upward)** start
for k = 2:nRows       % this for loop is shifting one element of a2 upword
    a2(j) = a2(k);
    j = j+1;
end
a2(5) = 0;            % assign last elements of a2 = 0 after SHIFT 1
disp(a2)              % display a2 after SHIFT 1
d2 = a1-a2;           % subtract a1 with a2 after **SHIFT 1**
d2 = abs(d2);         % again get the magnitude only
disp(d2)              % display d2 after SHIFT 1      
s2 = 0;               % intializing s2 for addition purpose
for l = 1:nRows       % this for loop is adding all elements of d2
    s2 = s2 + d2(l);
end
disp(s2)              % display s1 after adding all elements of d2

m = 1;                **% again this process continues till all elements of a2 shifted upward and subtracting with respect to a1.**
for n = 2:nRows
    a2(m) = a2(n);
    m = m+1;
end
a2(5) = 0;
disp(a2)
d3 = a1-a2;
d3 = abs(d3);
disp(d3)
s3 = 0;
for p = 1:nRows
    s3 = s3 + d3(p);
end
disp(s3)

q = 1;
for r = 2:nRows
    a2(q) = a2(r);
    q = q+1;
end
a2(5) = 0;
disp(a2)
d4 = a1-a2;
d4 = abs(d4);
disp(d4)
s4 = 0;
for s = 1:nRows
    s4 = s4 + d4(s);
end
disp(s4)

t = 1;
for u = 2:nRows
    a2(t) = a2(u);
    t = t+1;
end
a2(5) = 0;
disp(a2)
d5 = a1-a2;
d5 = abs(d5);
disp(d5)
s5 = 0;
for v = 1:nRows
    s5 = s5 + d5(v);
end
disp(s5)

w = 1;
for x = 2:nRows
    a2(w) = a2(x);
    w = w+1;
end
a2(5) = 0;
disp(a2)
d6 = a1-a2;
d6 = abs(d6);
disp(d6)
s6 = 0;
for y = 1:nRows
    s6 = s6 + d6(y);
end
disp(s6)

arr1 = [s1 s2 s3 s4 s5 s6];
disp(arr1)

minimum = min(arr1);
disp(minimum)

上面的代码将a2元素向上移动。 现在我的问题是,上面的代码正确吗?我的意思是,还有其他解决方案吗? 第二个问题是,我向上移动了a2元素。如何向下移动a2元素,并且在每次移位后遵循与上述相同的步骤?

请帮助!!!

2 个答案:

答案 0 :(得分:0)

  

我想上下移动a2的元素。

您可以使用for循环完成操作,但也可以使用screenshot

a2 = [9;4;2;3;5];
a2_1 = circshift(a2,-1); % shift upwards by 1 positions
a2_2 = circshift(a2,1); % shift a2 downwards by 1 positions

更新

  

但在将最后一个元素(即9)移位1后,该元素应为零,并且此过程继续进行。

您可以通过a2(5) = 0手动将最后一个元素设置为零,或者可以具有以下内容:

a2 = [9;4;2;3;5];
a2_1 = zeros(size(a2));
shiftupby = 1;
a2_1(1:numel(a2)-shiftupby) = a2(1+shiftupby:numel(a2));
shiftupby = -1;
a2_2 = zeros(size(a2));
a2_2(1-shiftupby:end) = a2(1:numel(a2)+shiftupby);

您还可以创建函数:

function v = shiftarray(a, k)
v = zeros(size(a));
n = numel(a);
if k > 0
    v(1:n-k) = a(1+k:n);
else
    v(1-k:end) = a(1:n+k);
end

然后

a2 = [9;4;2;3;5];
a2_1 = shiftarray(a2,-1); % shift upwards by 1 positions
a2_2 = shiftarray(a2,1); % shift a2 downwards by 1 positions

答案 1 :(得分:0)

您可以删除第一个元素,并在结尾添加零,例如:

% upward shift
a2 = [a2(2:end);0];

如果您希望k向上移动,可以执行以下操作:

a2 = [a2((k + 1):end);zeros(k,1)];

因此,下移可能是:

% downward shift
a2 = [0;a2(1:end-1)];

k下移:

% k downward shift
a2 = [zeros(k,1);a2(1:end-k)];