MATLAB数组的左右移动
生活随笔
收集整理的這篇文章主要介紹了
MATLAB数组的左右移动
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MATLAB數組的左右移動
- 說明
- MATLAB代碼
說明
我們在MATLAB的數組操作中,例如時域的平移,需要將數組向左/右平移N個單位。下方的代碼給出了一個簡單的操作函數。其中平移之后的補全采用了兩種方式,一種是移去的部分補到空缺處,保證平移之后數組長度不變;另外一種方式空白處補零。詳見代碼。
MATLAB代碼
%********************************************** %對一組數組進行左、右位移 %TowardSpring %2019年7月3日20:28:55 %輸入:BeforeArray:需要反轉的數組;direction:反轉的方向,1向左,2向右;len:位移長度;type:數據空缺補齊方式,1端點移位,2補零; %輸出:AfterArray:移位后的數據 %******************************************** function [AfterArray] = ArrayDisplacement(BeforeArray,direction,len,type) ArrayEnd = length(BeforeArray); if type == 1if direction == 1BeforeArrayL = BeforeArray(1:len);BeforeArrayR = BeforeArray(len+1:ArrayEnd);BeforeArrayL = fliplr(BeforeArrayL);BeforeArrayR = fliplr(BeforeArrayR);BeforeArray = [BeforeArrayL BeforeArrayR];AfterArray = fliplr(BeforeArray);elseBeforeArrayL = BeforeArray(1:ArrayEnd-len-1);BeforeArrayR = BeforeArray(ArrayEnd-len:ArrayEnd);BeforeArrayL = fliplr(BeforeArrayL);BeforeArrayR = fliplr(BeforeArrayR);BeforeArray = [BeforeArrayL BeforeArrayR];AfterArray = fliplr(BeforeArray); end elseif direction == 1BeforeArrayL = zeros(1,len);BeforeArrayR = BeforeArray(len+1:ArrayEnd);BeforeArrayL = fliplr(BeforeArrayL);BeforeArrayR = fliplr(BeforeArrayR);BeforeArray = [BeforeArrayL BeforeArrayR];AfterArray = fliplr(BeforeArray);elseBeforeArrayL = BeforeArray(1:ArrayEnd-len);BeforeArrayR = zeros(1,len);BeforeArrayL = fliplr(BeforeArrayL);BeforeArrayR = fliplr(BeforeArrayR);BeforeArray = [BeforeArrayL BeforeArrayR];AfterArray = fliplr(BeforeArray);end end總結
以上是生活随笔為你收集整理的MATLAB数组的左右移动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: querylist V4 列表采集
- 下一篇: KEBA控制器通过PROFINET连接西