Help with Matlab Problem: Commenting Code & Finding Errors

Click For Summary
SUMMARY

The forum discussion centers on a MATLAB code snippet that contains an infinite loop and several errors. Users emphasize the importance of commenting code for clarity and suggest improving indentation for better readability. The code initializes an array and attempts to display values while continuously resetting elements to zero, which leads to confusion. Participants encourage the original poster to analyze the code step-by-step and provide comments to facilitate understanding and debugging.

PREREQUISITES
  • Understanding of MATLAB syntax and structure
  • Familiarity with loops and conditional statements in programming
  • Basic knowledge of debugging techniques in MATLAB
  • Ability to write and interpret comments in code
NEXT STEPS
  • Learn MATLAB debugging techniques to identify and fix errors
  • Research best practices for commenting code in MATLAB
  • Explore MATLAB loop structures and their implications on program flow
  • Study the concept of infinite loops and how to avoid them in programming
USEFUL FOR

Students learning MATLAB, programmers seeking to improve their code readability, and anyone interested in debugging techniques for MATLAB scripts.

Aydashka
Messages
4
Reaction score
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
When is 0==0 ever not true?
 
Can you clarify your question please?
 
While 0==0. Means do the action for as long as zero equals zero
 
Right! Could you explain what this code does?
 
Aydashka said:
Right! Could you explain what this code does?
It's your schoolwork question -- what do you think it does and why?
 
If i knew what it did, i would'n ask about it here :biggrin:
 
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?
 
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   Reactions: 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.
 

Similar threads

Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K