Trouble with understanding MatLab code

AI Thread Summary
The discussion revolves around understanding a MATLAB code snippet that processes a matrix A and computes values stored in an array X. Initially, X is set to zero, and the code uses nested loops to sum the absolute values of the matrix's entries, ultimately aiming to find the maximum value in X. Participants clarify that the absolute function is applied to specific entries of the matrix, and suggest more efficient MATLAB commands to achieve the same results without using for loops. The conversation highlights the inefficiency of the original code and expresses frustration with the MATLAB class's effectiveness. Overall, the discussion emphasizes the importance of understanding matrix indexing and efficient coding practices in MATLAB.
mesa
Gold Member
Messages
694
Reaction score
36

Homework Statement


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 function 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).
 
Physics news on Phys.org
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?
 
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).
 
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!
 
I can't help but comment since this is one of the worst Matlab code I have ever seen.

Matlab:
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
Matlab:
A = [1 3; 2 4]; 
X = sum(abs(A),1);
F = max(X);

If X is not needed afterwards, it can simply be
Matlab:
A = [1 3; 2 4]; 
F = max(sum(abs(A),1));
 
  • Like
Likes RUber
mesa said:

Homework Statement



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 function of m and k?

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).
 

Similar threads

Back
Top