Relabel x-axis

1 次查看(过去 30 天)
Joe Bannen
Joe Bannen 2016-11-13
评论: Star Strider 2016-11-13
Hi
I am computing errors on a function and to ensure properly indexed data values have remapped my loop:
for n2=1:11
n=(n2-1)/10;
I am trying to to loop between 0 and 1 using 0.1 steps:
n= 0:0.1:1
Everything in the code works fine (I think!) At the end of my loop I have captured the data and attempt to plot.
I have used the simple command:
plot(error,'g')
This plots the error with the values n2 on the x-axis but I need them to show the values from n. The error does not need to change as this has been generated by the appropriate value (n) and not the index value (n2).
Does anyone know of a way to ensure the plot displays the correct value on the x-axis?
Best wishes
Joe
  2 个评论
Walter Roberson
Walter Roberson 2016-11-13
Please do not use error as the name of a variable, as it is a crucial MATLAB function for error control.
Joe Bannen
Joe Bannen 2016-11-13
Walter
The code uses multiple errors (none of them coded as error!) I shouldn't have included this in the forum, my apologies.
Joe

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2016-11-13
If ‘error’ is the same length as ‘n’, try this:
plot(n, error, 'g')
  9 个评论
Joe Bannen
Joe Bannen 2016-11-13
Hi
Thanks for that. A brilliant approach.
A quick point --> How could this be extended to look at 0.1 increments between 0 and 10 (or any other number)?
Cheers
Joe
Star Strider
Star Strider 2016-11-13
My pleasure. Thank you.
For creating vectors, there are two primary approaches depending on the result you want. I already illustrated them, so I’ll discuss them in a bit of detail.
  • If you want to define a vector with a fixed step (the length of the vector will vary to extend to the final value), use the colon operator;
  • If you want to define a vector with a fixed length (the constant step increment will vary to satisfy the length requirement), use the linspace function.
So for a fixed step of 0.1, use the colon operator:
start_value = 0;
final_value = 10:
step_size = 0.1
nv = start_value : step_size : final_value;

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by