Matlab 3D plot using arrays in if/else

In summary, the speaker is attempting to create a 3D graph using an array for x and y values and if/else statements for z values. However, the code has some issues that need to be addressed, such as defining the size of the arrays and correcting the if/else statement. Once these issues are fixed, the speaker should be able to successfully generate the desired graph.
  • #1
this town
1
0
Dear fellows,

I am trying to plot a 3D graph, determining the x and y values using an array and the z values using if/else. This is the code I am playing with but I can't get it to work:

xmin=-1000;
xmax=1000;
division=1.0;
num=((xmax-xmin)/division)+1;

for i=1:(num/2);
x(i)=i-(num/2);
for k=1:(num/2);
y(k)=k-(num/2);
for j=1:10:(num/2);
u(j)=j-num/2;
v(j)=j+6-num/2;
if ((x(i)>=u(j))&&(x(i)<=v(j))&&(x(i)<1000)&&(x(i)>0)&&(y(k)>0)&&(y(k)<1000)) |((x(i)>-1000)&&(x(i)<0)&&(y(k)>-1000)&&(y(k)<1000))|((x(i)>0)&&(x(i)<1000)&&(y(k)>-1000)&&(y(k)<0));
z(i,k)=1.0;
else
z(i,k)=0.0;
end
end
end
end

surf(x,y,z)

I am very new to this so any help would be very valuable, thanks!
 
Physics news on Phys.org
  • #2
It looks like you are attempting to create a 3D graph with x and y values determined by an array and the z values determined by an if/else statement. Unfortunately, your code has some issues that need to be corrected before it will work. First, you need to define the size of the arrays used for x and y. To do this, you can use the "num" variable you created as the size for both of these arrays. You can then use a loop to populate each array with the appropriate values. Second, you need to ensure that your if/else statement is correctly written. You have two separate conditions in the if statement that should be combined using the logical operator "and". Additionally, you should include parentheses around each condition to ensure that your statement is evaluated correctly. Finally, you need to make sure that the variables used in the if/else statement are correctly referenced. In particular, you should use the array indices to reference the x and y values, rather than the values themselves. Once all of these issues have been addressed, you should be able to generate a 3D graph with the desired result. Good luck!
 

1. How do I create a 3D plot using arrays in if/else statements in Matlab?

To create a 3D plot using arrays in if/else statements in Matlab, you can use the 'plot3' function and specify the x, y, and z coordinates as arrays. Then, use an if/else statement to determine the color or style of the plot based on a condition.

2. Can I use multiple if/else statements in my Matlab 3D plot?

Yes, you can use multiple if/else statements in your Matlab 3D plot to create more complex conditions. However, it is important to make sure that all if/else statements are properly nested and the conditions do not conflict with each other.

3. How can I change the colors of my 3D plot in Matlab using if/else statements?

You can change the colors of your 3D plot in Matlab using if/else statements by setting the 'Color' property of the plot to a variable that is assigned a specific color based on the condition in the if/else statement. You can also use 'ColorMap' to map a range of data values to different colors.

4. Is it possible to add a legend to my Matlab 3D plot with if/else statements?

Yes, you can add a legend to your Matlab 3D plot with if/else statements using the 'legend' function. You can specify the labels and colors of the legend based on the conditions in your if/else statements.

5. Can I create a 3D scatter plot in Matlab using arrays and if/else statements?

Yes, you can create a 3D scatter plot in Matlab using arrays and if/else statements by using the 'scatter3' function and specifying the x, y, and z coordinates as arrays. Then, use if/else statements to determine the size and color of the scatter points based on different conditions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
565
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
130
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
994
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top