# I was wondering if this code could be vectorized, and if so, can it be done without conditionals or loops? It would be great if the vectorized form could be written out so that I could run and follow

조회 수: 4(최근 30일)
Emily Iniguez 6 Jul 2020
편집: Emily Iniguez 7 Jul 2020
height = 666;
tri = ones(height);
start= length(tri) + 1;
for column = 1:length(tri)
tri(start:end, column)=0;
if column < (length(tri)/2)
*** no more help needed

### 답변(2개)

Bjorn Gustavsson 6 Jul 2020
Sure, you could generate 2 coordinate matrices x and y with meshgrid:
nPix = 66;
[x,y] = meshgrid(1:nPix);
Then you'll have two boundary-lines separating the high and low-level images, in order to set those you can do things like this:
Im = (1 - double(y<2*x))/2; % this is just for one of the boundaries but you can add that condition
And then add the random noise:
Im = Im + 0.5*rand(size(Im));
HTH

#### 댓글 수: 1

Emily Iniguez 6 Jul 2020
Thank you!

darova 6 Jul 2020
• use impoly to create triangle region
tri = ones(height);
imshow(tri)
p = impoly(gca,[0 1; 1/2 0; 1 1]*height);
imshow(tri1)
• second part of the code can be simplified
xmas = zeros(height);
xmas = abs(tri1 - noi);