MATLAB数组的左右移动
生活随笔
收集整理的這篇文章主要介紹了
MATLAB数组的左右移动
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
MATLAB數(shù)組的左右移動(dòng)
- 說(shuō)明
- MATLAB代碼
說(shuō)明
我們?cè)贛ATLAB的數(shù)組操作中,例如時(shí)域的平移,需要將數(shù)組向左/右平移N個(gè)單位。下方的代碼給出了一個(gè)簡(jiǎn)單的操作函數(shù)。其中平移之后的補(bǔ)全采用了兩種方式,一種是移去的部分補(bǔ)到空缺處,保證平移之后數(shù)組長(zhǎng)度不變;另外一種方式空白處補(bǔ)零。詳見(jiàn)代碼。
MATLAB代碼
%********************************************** %對(duì)一組數(shù)組進(jìn)行左、右位移 %TowardSpring %2019年7月3日20:28:55 %輸入:BeforeArray:需要反轉(zhuǎn)的數(shù)組;direction:反轉(zhuǎn)的方向,1向左,2向右;len:位移長(zhǎng)度;type:數(shù)據(jù)空缺補(bǔ)齊方式,1端點(diǎn)移位,2補(bǔ)零; %輸出:AfterArray:移位后的數(shù)據(jù) %******************************************** 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總結(jié)
以上是生活随笔為你收集整理的MATLAB数组的左右移动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: querylist V4 列表采集
- 下一篇: KEBA控制器通过PROFINET连接西