フィルターのクリア

how to shift arrays to the left

49 ビュー (過去 30 日間)
mary
mary 2013 年 1 月 30 日
if i have
a=[0 0 0 0 0 0 0 0]
a(1,8)=5;
shifting a will results in :
a=[0 0 0 0 0 0 5 0]
how can i do that?

採用された回答

Matt J
Matt J 2013 年 1 月 30 日
circshift(a,[0,-1])
  2 件のコメント
Matt J
Matt J 2013 年 1 月 30 日
If you always want the vacated edge of the matrix to be filled with zeros, you can use my noncircshift utility with the same syntax
function [B,src_indices,dest_indices]=noncircshift(A,offsets)
%Like circshift, but shifts are not circulant. Missing data are filled with
%zeros.
%
% [B,src_indices,dest_indices]=noncirchift(A,offsets)
%
%B is the resulting array and the other outputs are such that
%
% B(dest_indices{:})=A(src_indices{:})
siz=size(A);
N=length(siz);
if length(offsets)<N
offsets(N)=0;
end
B=zeros(siz);
indices=cell(3,N);
for ii=1:N
for ss=[1,3]
idx=(1:siz(ii))+(ss-2)*offsets(ii);
idx(idx<1)=[];
idx(idx>siz(ii))=[];
indices{ss,ii}=idx;
end
end
src_indices=indices(1,:);
dest_indices=indices(3,:);
B(dest_indices{:})=A(src_indices{:});
mary
mary 2013 年 1 月 30 日
thanx indeed

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by