Question about for loops and immultiply

9 views (last 30 days)
I wanted to edit a zeros matrix with a certain pattern of some various function, for this argument lets say that the function is a square (any geometrical pattern should be the same method?) I also wanted to implement immultiply, except i get a whitespace... Without using a for loop as such:
Z = zeros(800,600);
for m=275:325
for n=375:425
Z(m,n,:)=1
end
end
Is = immultiply(Id,Z); imshow(Is);
How do i go about it? because the for loop takes like 15 minutes..
for example I can make a diamond looking square but idk know a regular square equation at the center
[M N]= size(Id);
X=0:N-1;
Y=0:M-1;
[X Y]=meshgrid(X,Y);
Cx=0.5*N;
Cy=0.5*M;
square = (abs(X-Cx)/25+abs(Y-Cy)/25)
imshow(square)
figure
Is = immultiply(Id, square); imshow(Is);
Thanks, Benjy
  1 Comment
KSSV
KSSV on 1 Sep 2016
Edited: KSSV on 1 Sep 2016
What is Id in the above code?
Loop is taking much time because you are taking the output on the screen. Terminate the line Z(m,n,:)=1 ( Z(m,n,:)=1;). Off-course this can be done without loop.

Sign in to comment.

Accepted Answer

Thorsten
Thorsten on 1 Sep 2016
A fast method to create a white square is
Z(800,600)= 0;
Z(275:325, 375:425) = 1;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!