问题
I want to create a 2-D pulse in Octave. It will consist of a box in the middle with value 1.0 and 0.0 outside. The size of the 2-D array is 512. How can I do this in Octave?
回答1:
Given the width and height of this box stored in width and height, and assuming that they are both odd for symmetry, it's very simply:
row_half = floor(height/2);
col_half = floor(width/2);
pul = zeros(512,512);
pul(256-row_half:256+row_half, 256-col_half:256+col_half) = 1;
The first two lines of code determine what half the width and height of the defined box is. Next, we use the middle (256,256) and make sure we span from half the width both left and right, and half the height both top and bottom. This fills up the total area of your box and that is done by the indexing on the fourth line and we set these locations to 1.
The output 2D "pulse" is stored in the variable pul. A pre-condition of the above code is to make sure that your width and height are fully contained within the 512 x 512 grid. If not, Octave will give you an error going out of bounds.
Example
Let's say my width and height were both 101. We get this pulse, if we did imagesc(pul);
来源:https://stackoverflow.com/questions/31016858/creating-2-d-pulse-in-octave