Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Simple Matlab question

Status
Not open for further replies.

sgharagoz

Junior Member level 3
Joined
Aug 6, 2008
Messages
30
Helped
5
Reputation
10
Reaction score
4
Trophy points
1,288
Activity points
1,458
How can I do this in matlab?

a = [0 0 1 2 3]

shift to the left and get

a= [0 1 2 3 0]


cheers
 

How can I do this in matlab?

a = [0 0 1 2 3]

shift to the left and get

a= [0 1 2 3 0]


cheers

Code:
>> a=[ 0 1 2 3 0];
>> circshift(a,[1 1])

ans =

     0     0     1     2     3

circshift(A,shiftsize) circularly shifts the values in the array, A, by shiftsize elements. shiftsize is a vector of integer scalars where the n-th element specifies the shift amount for the n-th dimension of array A. If an element in shiftsize is positive, the values of A are shifted down (or to the right). If it is negative, the values of A are shifted up (or to the left). If it is 0, the values in that dimension are not shifted.
 
Hi
i=1;
a=[0 0 1 2 3]
s=size(a)
b=[a(i+1:s(2)), a(i)]
 

Code:
a = [0 0 1 2 3];
x=0;                             % circular shift up or down. up when positive, down when negative, no shift when 0;
y=-1;                           % circular shift left or right. right when positive, left when negative, no shift when 0;
a=circshift(a,[x y]);

Try it! Goodluck. :D
 
Is it possible to this?
first I have matrix X
X = [ 0 0 1 2 3];
and i want to shift it to left and replace the numbers by zero so I get

X = [3 0 0 0 0];
 

i = 1 % 'i' is positive integer range in X'range. and it is only works in left shift

Code:
X = [ 0 0 1 2 3];
X = horzcat(X(i+1:length(X)),zeros(1:i));
 

Is it possible to this?
first I have matrix X
X = [ 0 0 1 2 3];
and i want to shift it to left and replace the numbers by zero so I get

X = [3 0 0 0 0];


supports both left shift and right shift

Code:
%blooz Shift fill Zero 
function y=shift_filzero(x,n)

if n<0
    temp1=zeros(1,-n);
    temp2=circshift(x,[1 n]);
    temp3=[temp2(1:-n),temp1];
else

    temp2=circshift(x,[1 n]);
    temp3=[zeros(1,n),temp2(1:n)];
end

y=temp3


example

Code:
>> a=[ 0 1 2 3 4 5 6 7];

>> shift_filzero(a,4)

ans =

     0     0     0     0     4     5     6     7

>> shift_filzero(a,-4)

ans =

     4     5     6     7     0     0     0     0
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top