# Homework Help: Trouble with understanding MatLab code

1. Oct 8, 2014

### mesa

1. The problem statement, all variables and given/known data
A = [1 3; 2 4]; %Okay, so we are starting with this matrix
N = 2; %Here we are setting N equal to '2'
for k = 1:N %So here we have a 'for loop' for variable 'k' starting at 1 in increments of '1' up to 'N', which
%happens to be '2' so once this function gets to '2' it stops
X(k) = 0; %Sets some 'function X' with variable 'k' equal to '0'
end
%Our loop ends with X(1)=0 and X(2)=0
for k = 1:N %Start of a new loop, same as before.
for m = 1:N %Another loop written in our other loop.
X(k) = X(k) + abs(A(m, k)); %Here we are taking our X(k) from earlier? and now adding the absolute value of
%our matrix A as a fucntion of m and k?
end
end
F = X(N);
for k = 1:N-1
if X(k) > F
F = X(k);
end
end

I am having trouble understanding what this code is supposed to do. The comments are my attempt at trying to understand it and as you can see I am getting lost at the second loop and the 'abs' command (possibly messing up earlier).

2. Oct 8, 2014

### mesa

Wait a second, is the abs(A(m, k)) supposed to be the absolute value of matrix A and (m, k) the positions within the matrix?

3. Oct 8, 2014

### RUber

abs(A(m,k)) will return the absolute value of the (m,k)th entry in the matrix A.
k and m both index from 1 to 2.
X=(0,0) at the start of the double for loop that is troubling you.
for k = 1, see that you are adding |A(1,1)|+|A(2,1)| to get X(1)=|A(1,1)|+|A(2,1)|.
for k = 2, the same thing, X(2)=|A(1,2)|+|A(2,2)|.
The last for loop is going through each index to find the largest, one at a time, you could just write F=max(X).

4. Oct 8, 2014

### mesa

I kind of 'see' it.
The MatLab class I took at my University has been proving to be completely useless.

Either way, thanks for the tips!

5. Oct 9, 2014

### Staff: Mentor

I can't help but comment since this is one of the worst Matlab code I have ever seen.

Code (Matlab M):

A = [1 3; 2 4];
N = 2; %N is realted to the size of A, so should be derived from it:  N = size(A,1);
for k = 1:N
X(k) = 0;
end
% for loops can be very inneficient in Matlab, especially when they conatin an array that keeps increasing in size
% there is a built-in command for the purpose here:  X = zeros(1,N);

for k = 1:N
for m = 1:N
X(k) = X(k) + abs(A(m, k));
end
end
% again, for loops can be avoided:  X = sum(abs(A),1);

F = X(N);
for k = 1:N-1
if X(k) > F
F = X(k);
end
end
% as RUber said, this should be F = max(X);

All of this code can be replaced by
Code (Matlab M):

A = [1 3; 2 4];
X = sum(abs(A),1);
F = max(X);

If X is not needed afterwards, it can simply be
Code (Matlab M):

A = [1 3; 2 4];
F = max(sum(abs(A),1));

6. Oct 9, 2014

### Staff: Mentor

The MathWorks documentation of MatLab is pretty decent. Here's a link to their site: http://www.mathworks.com/help/matlab/index.html.

7. Oct 9, 2014

### RUber

I would be careful thinking of a matrix as a function of m and k. m and k are indices, they tell you what value of the matrix to return .
A(1,2) is the value of A in the 1st row, 2nd column. Same with X(k). X is not a function of k, X(k) is the kth entry of X. The first loop sets X=[0,0]. Then when you run for m = 1:2, X(1)=X(1)+A(m,1), you are saying: take the existing value of X(1) and add A(1,1) to it, then take the updated value of X(1) and add A(2,1) to it. The final value for X(1) at the end of the loop is 0 + A(1,1) + A(2,1). You also do the same for X(2).