In my linear algebra class we have a small component of learning Matlab with the occasional assignment.
The question asked to modify an existing (not shown) $25 \times 25$ matrix with the following conditions:
- if an entry in the matrix $B$ is $\geq 0$ then multiply by $4$
- if an entry in the matrix $B$ is $<0$ then add $6$ to it
I created a function file in Matlab by doing the following:
function [A] = modify_matrix(B, n, m),
A = zeros(n,m);
for i = 1:n,
for j = 1:m,
if B >= 0,
A = B*4;
else,
A = B+6;
A(i,j) = B(i,j);
end
end
end
end
This simply reproduces the same matrix $B$.
Any suggestions?
Using Matlab vectorization tricks you can do that in one line: