last number is out of matrix range range

조회 수: 1 (최근 30일)
Abdulaziz
Abdulaziz 2013년 9월 23일
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에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by