last number is out of matrix range range

2 views (last 30 days)
Abdulaziz
Abdulaziz on 23 Sep 2013
Hi guys,
I need your help with this problem please, n1=492;n2=354;% first and last elements of x X=n1:(n2-n1)/(length(x)*1.25):n2; where x=[492 486 483 476 474 474 472 468 466 460 456 456 454 452 444 440 438 432 428 424 420 418 414 411 408 400 388 374 354];
the problem is that the last element of X(X(end)=3.549517241379310e+002) will be a little bit greater than n2= 354 ( the last element of x) is there any way to fix this problem to get (X(end)=n2=x(end))

Answers (3)

Image Analyst
Image Analyst on 23 Sep 2013
Can't you just use linspace???
outputArray = linspace(startingValue, endingValue, numberOfElements);

David Sanchez
David Sanchez on 23 Sep 2013
you can round the last element:
n1=492; n2=354; x = ones(20,1); X=n1:(n2-n1)/(length(x)*1.25):n2;
X2 = floor(X);
X2 =
Columns 1 through 9
492 486 480 475 469 464 458 453 447
Columns 10 through 18
442 436 431 425 420 414 409 403 398
Columns 19 through 26
392 387 381 376 370 365 359 354
  1 Comment
Abdulaziz
Abdulaziz on 23 Sep 2013
Thank you David for your answer. I thought there will be a way to control the step size so that last element of X to be the same as last element of x. I forgot to include the x i am sorry. x=[492 486 483 476 474 474 472 468 466 460 456 456 454 452 444 440 438 432 428 424 420 418 414 411 408 400 388 374 354];

Sign in to comment.


Abdulaziz
Abdulaziz on 23 Sep 2013
I found this
X=n1:(n2-n1)/floor(length(x)*1.25):n2
if we put floor into the step we are forcing the step to be integer number and thus the last elements of x and X will be the same

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!