How to shift all of the contents of an array to the right by some value.
102 views (last 30 days)
Show older comments
John Lawler
on 4 Mar 2015
Commented: Image Analyst
on 4 Mar 2015
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.
0 Comments
Accepted Answer
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
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?
More Answers (1)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!