matlab matrix substitution without loops

476 Views Asked by At

below there is a task we were asked to do using matlab. I wrote this function below which works fine. But, since 'for' loops were not shown yet in the course, I think that the instructor meant doing it without loops. Any ideas? Thanks!

By the way, How do I make new line in stackexchange editor? Thanks!

enter image description here Question 2

1

There are 1 best solutions below

0
On BEST ANSWER

Given $m$, $n$ you can assign

$A(1:2:m,1:2:n)=1;$

$A(2:2:m,2:2:n)=1;$

P.S. Loops in MATLAB are in general slow, substitute it if possible by vector/matrix operations that MATLAB is optimized for.