Trouble with understanding MatLab code

Click For Summary

Discussion Overview

The discussion revolves around understanding a piece of MatLab code related to matrix operations, specifically focusing on the use of loops, absolute values, and indexing. Participants are trying to clarify the functionality of the code and its efficiency.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the purpose of the code, particularly regarding the second loop and the 'abs' command.
  • Another participant clarifies that abs(A(m, k)) returns the absolute value of the (m, k)th entry in the matrix A, and explains how the values of X are computed in the nested loops.
  • A different participant critiques the code's efficiency, suggesting that the size of N should be derived from the matrix A and that built-in functions could replace the loops for better performance.
  • There is a suggestion that the code could be simplified significantly using built-in MatLab functions like sum and max.
  • One participant warns against thinking of a matrix as a function of indices, emphasizing that m and k are simply indices that access specific values in the matrix.

Areas of Agreement / Disagreement

Participants express varying levels of understanding and critique of the code. While some agree on the inefficiency of the original code and propose alternatives, others focus on clarifying the logic behind the existing code without reaching a consensus on the best approach.

Contextual Notes

There are unresolved questions regarding the clarity of indexing in matrices and the implications of using loops versus built-in functions in MatLab. The discussion reflects a range of familiarity with MatLab coding practices.

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   Reactions: 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

Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
867
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K