Efficiently Set a Sequence in Matlab: Quick Question Answered

  • MATLAB
  • Thread starter lewdtenant
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses how to set a variable with 9 increments and how to plot more points on the x and y axes in MATLAB. The method of using linspace and changing the tick marks is suggested.
  • #1
lewdtenant
63
1
Matlab --> quick question

Homework Statement



suppose I want to set a = [0:64] with an output of 9 increments. For the life of me I cannot remember how to do this other than doing it brute force style.


Homework Equations





The Attempt at a Solution



since 64/9=7.11 then, a =[0:7.11:64] gives me 9 increments but I know there's a better way to do this. please help, thanks.
 
Physics news on Phys.org
  • #2
help linspace
 
  • #3
yes, thanks
 
  • #4
is there a way that i can plot more points in the x-axis and the y-axis? instead of going by 5 -10 -15- 20 and go 5 - 6 - 7 - 8 - 9...20 .
 
  • #5
well, I'm no MATLAB expert, but here goes.

When you specify x and y, making x= [5:1:20] will give you 5, 6, 7, 8... 20. you can do this for y also. then when you plot (x, y) it should give you what you need.

i hope that helps, let me know if i misunderstood your question.
 
  • #6
if i plot x= [5:1:20] and y= [5:1:20], the axes spacing goes 5,10,15,20, but i would like to make it go 1 by 1. thus showing more points in the plot.

not sure if that is possible, but that´s why I am asking i guess.

thanks for your help btw
 
  • #7
tko_gx said:
if i plot x= [5:1:20] and y= [5:1:20], the axes spacing goes 5,10,15,20, but i would like to make it go 1 by 1. thus showing more points in the plot.

not sure if that is possible, but that´s why I am asking i guess.

thanks for your help btw

I think you're just referring to the tick marks on the axis. The distribution of tick marks is not related to the number of points plotted on the graph. You can change the tick mark distribution either going to 'axis properties' in the 'edit' menu of the plot, or you can define them from the command line. For example,
Code:
x = -pi:.1:pi;
y = sin(x);
plot(x,y)
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})
defines the tick placement and the labels.
 
  • #8
thank you very much. that's exactly what i meant to say.
 

1. How can I efficiently set a sequence in Matlab?

There are a few ways to efficiently set a sequence in Matlab. One option is to use the colon operator, for example, a = 1:10 will create a sequence of numbers from 1 to 10. Another option is to use the linspace function, which allows you to specify the number of points you want in your sequence, for example, a = linspace(1,10,5) will create a sequence of 5 evenly spaced numbers between 1 and 10.

2. Can I set a sequence with non-numeric values in Matlab?

Yes, you can use the colon operator or the linspace function to create a sequence with non-numeric values. For example, a = 'a':'e' will create a sequence of letters from 'a' to 'e'.

3. How do I set a sequence in reverse order in Matlab?

To set a sequence in reverse order in Matlab, you can use the colon operator with a negative step size. For example, a = 10:-1:1 will create a sequence of numbers from 10 to 1 in reverse order. You can also use the fliplr function to reverse the order of a sequence created with the linspace function.

4. Is there a limit to the length of a sequence I can create in Matlab?

There is no specific limit to the length of a sequence you can create in Matlab. However, keep in mind that very long sequences may take longer to compute and may use more memory.

5. Can I use variables in a sequence in Matlab?

Yes, you can use variables in a sequence in Matlab. For example, you can set a = 1 and b = 5, and then create a sequence using those variables, such as c = linspace(a,b,10) which will create a sequence of 10 numbers between 1 and 5.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Introductory Physics Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
Back
Top