如何移动矩阵中的列(左或右)



我想对矩阵进行非循环移位,然后向左或向右填充零(取决于移位(,即如果矩阵向右移位,则会向左填充零。

我正在使用MATLAB 2019b,到目前为止我的代码如下:

%dummy data
data = rand(5, 16);
channelSink = 9; %this variable will either be >layerIV, <layerIV or =layerIV
layerIV = 7;
diff = layerIV - channelSink;
for channel = 1:16

if channelSink > layerIV 

%shift columns to the left by ab(diff)
%and
%set columns shifted by ab(diff) to zero

elseif channelSink < layerIV

%shift columns to the right by diff
%and
%set columns shifted by diff to zero

else %idiff = 0, don't shift

diff = 0; 
disp('Sink at channel 7; not necessary to re-align');

end

end

提前感谢

这将矩阵data水平移动d位置,如果d为正,则向右移动,如果为负,则向左移动,并在另一侧填充零:

data = rand(5, 16); % example matrix
d = 3; % shift; positive/negative for right/left
result = zeros(size(data), 'like', data); % preallocate with zeros
result(:,max(1,1+d):min(end,end+d)) = data(:,max(1,1-d):min(end,end-d)); % write values

相关内容

  • 没有找到相关文章

最新更新