How to Adjust MATLAB Code to Correctly Draw a Diamond with Even Sizes?

Click For Summary

Discussion Overview

The discussion revolves around adjusting MATLAB code to correctly draw a diamond shape based on user-defined sizes. The focus is on addressing issues that arise specifically with even sizes, leading to extra asterisks appearing in the output.

Discussion Character

  • Technical explanation
  • Debugging
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach to drawing a diamond using a series of loops and conditional statements in MATLAB.
  • Another participant suggests that the issue with extra asterisks arises from multiple if statements being executed simultaneously in the loops.
  • A participant expresses uncertainty about the exact location of the problem and requests clarification.
  • One participant emphasizes the importance of using MATLAB's debugging features, particularly breakpoints, to identify where the code fails.
  • Another participant identifies that the problem may stem from the first line of the diamond where certain conditions overlap, leading to unintended behavior.
  • One suggestion is made to use elseif statements to prevent multiple if statements from executing at the same time.
  • A final participant confirms that a proposed solution resolves the issue effectively.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the problem and potential solutions, but there are multiple approaches suggested for resolving the issue, indicating that the discussion remains somewhat open-ended.

Contextual Notes

Participants discuss specific conditions in the code that lead to the problem, but the exact mathematical reasoning and implications of the proposed changes are not fully resolved.

Dell
Messages
555
Reaction score
0
i need to build a code which asks the user for the size of the desired diamond and then draws it, i can get this to workl perfectly by using the fprintf command and building the diamond out of 4 parts,: see code-

Code:
clc
clear
h=input('enter height of diamond :');
y=1;
while y<=h/2
    for x=1:h-1
        if x<=((h)/2-(y-1))
            fprintf(1,'*\t');
        end
        if x>(h)/2-(y-1) & x<(h)/2+(y-1)
            fprintf(1,'\t');
        end
        if x>=((h)/2+(y-1)) &x<=h-1
            fprintf(1,'*\t');
        end
    end
    y=y+1;
    fprintf(1,'\n')
end

while y>h/2 &y<=h
    for x=1:h-1
        if x<=((h)/2-(h-y))
            fprintf(1,'*\t');
        end
        if x>(h)/2-(h-y) & x<(h)/2+(h-y)
            fprintf(1,'\t');
        end
        if x>=((h)/2+(h-y)) &x<=h-1
            fprintf(1,'*\t');
        end
    end
    y=y+1;
    fprintf(1,'\n')
end

but for any even numbers i put in as my size, i get 2 extra asterixs on the corners, can anyone see how i chan get rid of them?
 
Physics news on Phys.org
Look inside the loops that create the horizontal rows of asterisks. You actually have a degenerate case where two of the if statements get executed! (At the very top and very bottom rows)
 
thats what i thought must be happening, but i cannot find where, could you show me
 
I think I mentioned this in a different thread to you, but it's absolutely essential to learn how to use the debugging features of MATLAB, and especially breakpoints:
http://ieee-uffc.org/ultrasonics/software/MATLAB/Lecture8/Lecture8_2.htm
http://en.wikibooks.org/wiki/MATLAB_Programming/Debugging_M_Files

By stepping through, you'll find that the two cases are happening right at the middle (h/2) of the line. Use this information to learn how to use breakpoints.
 
i think that the problem comes fro the 1st line, where (y-1)=0 therefore, (h)/2-(y-1) is equal to x>(h)/2+(y-1) and so both the 1st and 3rd if commands are executed,
so i tried making one <= and one > but that messed up my whole diamond
 
Use some elseif statements to prevent the execution of more than one of the if statements:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/brqy1c1-1.html#brqy1c1-3

You could use a break statement, but this is considered very poor programming form.
 
Last edited by a moderator:
thanks, works pefrectly
 

Similar threads

Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K