Shifting Vector Elements Left in MATLAB

In summary: You will need to loop through the original vector a(i) and assign the values to the new vector b(i+n). You will also need to consider what happens when you reach the end of the vector a(i) and need to wrap around to the beginning.
  • #1
senthya
2
0

Homework Statement



you are given a vector y construct a MATLAB script program to shift all element of y left by ( n ) position..

I mean if y=[1 5 6 9 3]
and n=2

by apply this program on y we have :

y=[3 1 5 6 9]


The Attempt at a Solution



this is my Attempt to solve this problem :

x=input('x= ')
n=input('n= ')
for i=1:n
x(i)=1;
while x(i)+1<=n
x(i)=x(i)+1;
end
end
x

I know its wrong ..to that reason i need your help..
 
Physics news on Phys.org
  • #2
x(i) = 1 is changing your data into a string of 1's

while x(i)+1<=n -- This actually checks to see if the value of x(i) + 1 <= n. I'm not sure how this helps you?
 
  • #3
Welcome to PhysicsForums, senthya!

Here are some hints:
You need to do index shifting
prod(size(matrix_A)) gives the number of elements in matrix_A

You can use many C/C++ constructs in MATLAB, including if, while, for:
http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f4-1931.html
 
Last edited by a moderator:
  • #4
Feldoh: thank you for your comment..

MATLABdude : thank you for your help but i still don't know how to solve this problem because I am just a beginner in MATLAB

I tried again and this is my attempt :

x=input('x= ')
n=input('input n less than x ')
m=length(x)
n=[x(1,n+1:m),x(1,1:n)]
for i=1:20
for j=1:20
if i>=j
a(i,j)=i+j
else
a(i,j)=0
end
end
endPlease tell ma how to solve it..
with respect to you..
 
  • #5
Your new program is overly complicated and still doesn't solve the problem (also, it just zeros out a bunch of entries--have you actually tried running the script? That should tell you that you've got an error.) Here at PhysicsForums, we don't tell you how to solve a problem, but rather try to guide you through it.

So I suspect that you probably just jumped into the coding for this question without thinking of how to actually do it (don't feel too bad, this is what most new programmers do). Let's start there: if you were doing this by hand, what would you have to do?

Also, for a 1-D array, you don't need to do a(1, index) to address something, you can just use a(index).
 
  • #6
To further that MATLABdude said, it's always good practice to write out your algorithms in pseudo-code before you try implementing them.

That said let's talk through it a little bit:
You want to take a 1-dimensional matrix (row vector) and you want to move the values n spaces:

In other words you are trying to make a new vector b(i) such that: b(i+n) = a(i) for all i

What's your next step in logically doing this going to be?
 

Related to Shifting Vector Elements Left in MATLAB

What is "shifting vector elements left" in MATLAB?

Shifting vector elements left in MATLAB refers to the process of moving the contents of a vector to the left by a certain number of positions. This can be useful for reorganizing data or manipulating arrays in a specific way.

How do I shift vector elements left in MATLAB?

To shift vector elements left in MATLAB, you can use the built-in function "circshift" with the vector and the desired number of positions as inputs. Alternatively, you can use indexing and assignment to manually shift the elements.

Can I shift vector elements left by a fraction of a position in MATLAB?

No, since vectors in MATLAB are discrete data structures, they can only be shifted by whole number positions. However, you can interpolate data to achieve a similar effect.

What happens to the elements that are shifted off the vector?

When you shift vector elements left in MATLAB, the elements that are shifted off the vector are lost. They are not stored anywhere and cannot be retrieved.

Is it possible to shift vector elements left in a circular manner in MATLAB?

Yes, the "circshift" function in MATLAB allows for circular shifting of vector elements by wrapping around the vector to the other side. This can be useful for creating circular buffers or implementing circular operations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
841
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
972
  • Engineering and Comp Sci Homework Help
Replies
2
Views
867
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
976
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
924
Back
Top