last number is out of matrix range range

1 次查看(过去 30 天)
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))

回答(3 个)

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

David Sanchez
David Sanchez 2013-9-23
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 个评论
Abdulaziz
Abdulaziz 2013-9-23
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];

请先登录,再进行评论。


Abdulaziz
Abdulaziz 2013-9-23
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by