How to shift all of the contents of an array to the right by some value.

102 views (last 30 days)
Good evening. First of all I am a SUPER novice user, and this is my second time ever using MATLAB.
I have an array that is generated from an image. Its basically a normal distribution 1x7944 or so.
I would like to take this array and shift all of the elements to the right, and pad the initial values with zeros.
A=[1,1,1,1,1]
B=A+Offset
B=[0,0,0,0,1,1,1,1,1]
basically something like that, where I make a new matrix that contains the original one, Offset to the right a bit. Adding in a few extra columns of zeros.
My ultimate goal is to write a code that looks at the original matrix, and then finds the optimum offset so that when I add the two matrices together I get the flattest possible answer. I would like Matlab to figure out this value
Example:
Matrix A=[0,1,2,3,4,5,6,5,4,3,2,1,0]
The optimal solution would be to shift this matrix by 3, creating
Matrix B=[0,0,0,0,1,2,3,4,5,6,5,4,3,2,1,0]
C=A+B
C= [0,1,2,4,6,8,10,10,10,8,6,4,2,1,0]
Note that this answer has the largest number of 10's, making the widest horizontal peak in the data, which is what I want to figure out with a much larger data set of 8 bit numbers. (0-255)
How is the best way to do this in Matlab?
Thank you.

Accepted Answer

Image Analyst
Image Analyst on 4 Mar 2015
Just prepend a zero array of the desired length:
B = [zeros(1,3), A];
If you're going to add A to B, then you need to append the same number of zeros to A because they need to be the same length if you're going to add them together.
Aprime = [A, zeros(1,3)];
C = B + Aprime;
  2 Comments
John  Lawler
John Lawler on 4 Mar 2015
Working Great! Thanks!
How are you with the while loop aspect of this question? Actually having matlab iterate guessing at the value of "offset" to provide the minimum std deviation of the flat top? I can post the whole code if you would like, but it is using the image analysis toolkit, which I'm not sure everybody has. I will post up the code, nothing that an offset value of 3500 seems pretty good to me, but not optimal.
Image Analyst
Image Analyst on 4 Mar 2015
If you want the min stddev, why not try stdfilt()? It will tell you the stddev in a sliding window. What is the use case for this algorithm? Why do you want to add an image to itself to find the flattest sum?

Sign in to comment.

More Answers (1)

John  Lawler
John Lawler on 4 Mar 2015
Here is an image for you to play around with. select the top left with a mouse click, and then the bottom right with a second mouse click when the program prompts you to.
Thanks!

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!