Solving MATLAB Duplication Error with For Loop

  • MATLAB
  • Thread starter Superposed_Cat
  • Start date
  • Tags
    Error Matlab
In summary, the for loop in MATLAB executes 10 times and displays the absolute tangent values for each iteration. However, there seems to be an issue with the display as it shows the same values twice. It is suggested to use "clc" before the script to clear the command window before running the code again.
  • #1
Superposed_Cat
388
5
Hi, I'm totally new to MATLAB and tried this for loop

Code:
for K = 1:10
  disp(abs(tan(K)))
end

it displays
Code:
1.5574
 2.1850
 0.14255
 1.1578
 3.3805
 0.29101
 0.87145
 6.7997
 0.45232
 0.64836

 1.5574
 2.1850
 0.14255
 1.1578
 3.3805
 0.29101
 0.87145
 6.7997
 0.45232
 0.64836

Why did it display this twice?
Thanks for any help.
 
Physics news on Phys.org
  • #2
The shortest answer is that it only displayed it once for me when I ran the same code in R2012a.
 
  • #3
Thanks, When I did it again it worked without changing anything but it happened again and duplicated a graph.
 
  • #4
If you don't show me anything to go with, I can only assume you sneezed while hitting enter, or that you were drunk and seeing double.
 
  • #5
lol, the button only allows you to press it once before changing to a animated non-clickable gear.
 
  • #6
try "clc" before the script
 
  • #7
whats that do?
 

Related to Solving MATLAB Duplication Error with For Loop

What causes a duplication error in MATLAB when using a for loop?

A duplication error in MATLAB when using a for loop is caused by attempting to assign the same value to multiple elements of an array within the loop. This can occur if the loop variable is not properly incrementing or if the loop is not properly accessing the elements of the array.

How can I troubleshoot a duplication error in MATLAB when using a for loop?

To troubleshoot a duplication error in MATLAB when using a for loop, you can start by checking the loop variable to ensure it is properly incrementing. You can also check the indexing of the array to make sure the elements are being accessed correctly. Additionally, you can use the debugger tool in MATLAB to step through the loop and identify any errors.

Can I use a while loop instead of a for loop to avoid duplication errors in MATLAB?

Yes, a while loop can be used instead of a for loop to avoid duplication errors in MATLAB. While loops can be more flexible in terms of incrementing and accessing elements, which can help prevent duplication errors. However, it is important to ensure the loop is properly constructed to avoid potential errors.

How can I prevent duplication errors in MATLAB when using a for loop?

To prevent duplication errors in MATLAB when using a for loop, it is important to carefully construct the loop and ensure that the loop variable is properly incrementing and that the array elements are being accessed correctly. It can also be helpful to use the debugger tool to identify and fix any potential errors.

Are there any built-in functions in MATLAB that can help prevent duplication errors when using a for loop?

Yes, there are built-in functions in MATLAB that can help prevent duplication errors when using a for loop. These include functions such as "unique" and "ismember" which can help identify and remove duplicate values in arrays. Additionally, the "break" and "continue" statements can be used within a for loop to prevent duplicate assignments to array elements.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
Back
Top