Help with Matlab Problem: Commenting Code & Finding Errors

In summary, this code does something to each line in a list. It starts at line 1 and goes to line Maxi. It then assigns a value to p on line 2.
  • #1
Aydashka
4
0
New poster has been reminded to show their work on schoolwork problems
Hi! I really need help with the following Matlab problem.
What does this code do? I need to comment what the important lines of this code do. Also the code contains several errors which i need to find. Here is the program:
Matlab:
clear all; close all; clc;
Maxi=50; tab=[1 :Maxi] ; tab(1)=0;

while 0==0
  i=1;
  while tab(i)==0
     i=i+1;
  end
  p=tab(i) ; disp( [num2str(p)] ) ;
  for k=p:p:Maxi
     tab(k)=0;
  end
end
Thank you so much in advance!
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
When is 0==0 ever not true?
 
  • #3
Can you clarify your question please?
 
  • #4
While 0==0. Means do the action for as long as zero equals zero
 
  • #5
Right! Could you explain what this code does?
 
  • #6
Aydashka said:
Right! Could you explain what this code does?
It's your schoolwork question -- what do you think it does and why?
 
  • #7
If i knew what it did, i would'n ask about it here :biggrin:
 
  • #8
If I see the literal expression 1+1 in my code what could I replace it with?

I would replace it with 2 as its equivalent and there’s little need to complicate my code with 1+1 even though I know the compiler, kindhearted though it is, would replace it with 2 for me,

Similarly, If I see 0==0 in my code what can I replace it with?

How can you define an infinite loop?
 
  • #9
Aydashka said:
If i knew what it did, i would'n ask about it here :biggrin:

Poor answer.

I adjusted your code with some spacing to make it easier to read.

You have MATLAB and so you need to step through your code and discover what it does. This is what programmers do all the time with new unknown code.
 
  • Like
Likes FactChecker
  • #10
Since this is a probably homework problem, we are only allowed to give hints and advice to lead you to the solution. The first thing I would advise you to do is to improve the indentation so that the loops are easy to see where they begin and end. Then I would advise you to write some comments in the code about what each line and loop would do. After you post that, we can review your comments and give you some more advice.

PS. If there is something that you expected the code to do, you should describe that in comments at the appropriate places in the code. As far as I can see, the current code is nonsense.
 

FAQ: Help with Matlab Problem: Commenting Code & Finding Errors

What is the purpose of commenting code in Matlab?

Commenting code in Matlab is important because it helps other users understand the purpose and functionality of the code. It also helps the original coder remember what they were trying to accomplish and makes it easier to make changes in the future.

How do I comment code in Matlab?

To comment code in Matlab, you can use the percent symbol (%) at the beginning of a line to indicate that it is a comment. You can also use the %{ and %} symbols to comment out multiple lines of code.

How can I find errors in my Matlab code?

There are a few ways to find errors in Matlab code. One way is to use the built-in debugging tools, such as the "dbstop if error" command, which will stop the code at the point where an error occurs. You can also use the "try" and "catch" statements to catch and handle errors in your code.

What is the purpose of using breakpoints in Matlab?

Breakpoints in Matlab are used to pause the execution of the code at a specific point. This allows you to inspect the values of variables and step through the code line by line, which can help in identifying errors and understanding the flow of the code.

How can I improve the efficiency of my Matlab code?

To improve the efficiency of your Matlab code, you can use vectorization, which involves performing operations on entire arrays instead of individual elements. You can also preallocate memory for variables, avoid unnecessary loops, and use built-in functions instead of writing your own. Additionally, commenting your code and using meaningful variable names can also help improve the readability and efficiency of your code.

Similar threads

Replies
3
Views
1K
Replies
23
Views
7K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
6
Views
2K
Replies
1
Views
1K
Back
Top