How can I shift Right which is an array of numbers?

I need is a new shifted array and eventually I want to add the arrays.(New Shifted array + Original Array). Can I do something like e.g . aa =[11 22 33 44]; bb = aa(2:4); % this is shifting left It gives bb = [22 33 44], but one value is dropped out. If possible can someone provide method to shift right and how to pad the dropped out value appropriately so that the size of the new array remains same as the original array?

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 14 de Feb. de 2013
Editada: Azzi Abdelmalek el 14 de Feb. de 2013
use circshift function
circshift(aa,[0 -1])
or
circshift(aa,[0 1])

7 comentarios

Micky
Micky el 14 de Feb. de 2013
Thanks Azzi for the response, but there is no change in the new array
Azzi Abdelmalek
Azzi Abdelmalek el 14 de Feb. de 2013
Editada: Azzi Abdelmalek el 14 de Feb. de 2013
aa =[11 22 33 44]
out=circshift(aa,[0 -1])
out and aa are the same?
If it's not what you need then what should be the result for this case?
I am trying something like this:
x= [11;22;33;44;55;66]
x =
11
22
33
44
55
66
>> k=2 % desired number of shifts
k =
2
>> s = x([end-k+1:end 1:end-k])
s =
55
66
11
22
33
44
Here, it is working fine but I do not want the shifted out values to come circularly back to front of the array, I want to pad the number of shifts by my own desired values like zeros etc. Can you suggest some change?
x= [11;22;33;44;55;66]
circshift(x,[2 0])
Micky
Micky el 14 de Feb. de 2013
Editada: Micky el 14 de Feb. de 2013
This is what I got
x= [11;22;33;44;55;66]
circshift(x,[2 0])
x =
11
22
33
44
55
66
The shifted values are circulated back to the front, and that is what I dont want. ans =
55
66
11
22
33
44
Azzi Abdelmalek
Azzi Abdelmalek el 14 de Feb. de 2013
Editada: Azzi Abdelmalek el 14 de Feb. de 2013
n=2
x= [11;22;33;44;55;66]
x=circshift(x,[n 0])
x(1:n)=0 % or what you want
Micky
Micky el 14 de Feb. de 2013
Thanks for the help Azzi.

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 14 de Feb. de 2013
Not quire sure what you're describing about not dropping off any values and having bb be the same size as aa, but how about this:
aa =[11 22 33 44];
bb = zeros(size(aa))
bb(2:4) = aa(2:4)
% or
bb1 = zeros(size(aa))
bb1(1:3) = aa(2:4)
In the command window:
bb =
0 22 33 44
bb1 =
22 33 44 0
You can see there is a zero and no value is dropped off or lost and the size of bb is the same as aa. If one of those methods is not what you want, explain in more detail.

3 comentarios

Thanks for the response. I am trying something like this:
x= [11;22;33;44;55;66]
x =
11
22
33
44
55
66
>> k=2 % desired number of shifts
k =
2
>> s = x([end-k+1:end 1:end-k])
s =
55
66
11
22
33
44
Here, it is working fine but I do not want the shifted out values to come circularly back to front of the array, I want to pad the number of shifts by my own desired values like zeros etc. Can you suggest some change?
I am trying out your suggestion as well. May be the above description helps you understand the problem more in detail.
Image Analyst
Image Analyst el 14 de Feb. de 2013
Editada: Image Analyst el 14 de Feb. de 2013
My solution will work. You just have to generalize it to "k" instead of fixed numbers of 2:4 like your example.
x = [11;22;33;44;55;66]
% k = -2;
k = +2;
s = zeros(size(x));
if k >= 1
s(k+1:end) = x(1:end-k)
elseif k <= -1
s(1:end+k) = x(-k+1:end)
end
Micky
Micky el 14 de Feb. de 2013
Thanks for the help.

Iniciar sesión para comentar.

komal
komal el 14 de Jun. de 2019

0 votos

Ques= x =[11;22;33;44]
i want this results
x = [0;11;22;33;44]
How can i do it:

Categorías

Productos

Preguntada:

el 14 de Feb. de 2013

Comentada:

el 26 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by