Trouble with understanding MatLab code

I hope this makes it clearer. In summary, the code is using for loops to add the absolute values of each column in matrix A to an array X, and then finding the maximum value in X.
  • #1
mesa
Gold Member
695
38

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
  • #2
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
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
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
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
  • #7
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).
 

Related to Trouble with understanding MatLab code

1. How do I troubleshoot errors in my MatLab code?

When encountering errors in your MatLab code, the first step is to carefully read the error message and try to identify the specific line or lines of code that are causing the issue. You can also use the debugging tools in MatLab, such as the "dbstop" function, to stop the code at a certain point and investigate the values of variables. Additionally, checking your syntax and making sure all variables are properly defined can help prevent errors.

2. Why is my MatLab code not producing the expected output?

There could be several reasons why your MatLab code is not producing the expected output. Some common causes include errors in your code, incorrect input values, or an issue with your algorithm. It is important to carefully review your code and input values to ensure they are correct. You can also use the debugging tools in MatLab to step through your code and check the values of variables to identify any issues.

3. How can I improve the efficiency of my MatLab code?

To improve the efficiency of your MatLab code, you can try implementing vectorization, which is a technique that uses array operations instead of loops to perform calculations. This can significantly speed up your code. You can also use the profiler tool in MatLab to identify any bottlenecks in your code and optimize them. Additionally, avoiding unnecessary operations and minimizing the use of memory-intensive functions can also improve efficiency.

4. What resources are available to help me understand MatLab code?

There are various resources available to help you understand MatLab code, including the MatLab documentation, online tutorials, and forums where you can ask for help from the MatLab community. Additionally, there are also books and courses specifically designed to teach MatLab programming and troubleshooting techniques.

5. How can I prevent errors in my MatLab code?

To prevent errors in your MatLab code, it is important to follow good coding practices, such as using meaningful variable names, commenting your code, and testing your code with different input values. You can also use the MatLab debugger to identify and fix errors before running your code. Regularly checking for updates and using the latest version of MatLab can also help prevent errors caused by outdated functions or commands.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
965
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top