Creating 2-D Pulse in Octave

只谈情不闲聊 提交于 2019-12-12 06:11:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!