using linspace in a matrix incrementing by a certain number

in a row of a matrix I need start with 51 and subtract 7.25 each time, 4 times, so the row would look like
51 43.75 36.5 29.25 22
I have
A=[2 4; 6 8]
B=[A(1,2:-1:1) A(1,2:-1:1) ; A(2,2:-1:1) A(2,2:-1:1);linspace(51,22,4)]
but the result is
A =
2 4
6 8
B =
4.0000 2.0000 4.0000 2.0000
8.0000 6.0000 8.0000 6.0000
51.0000 41.3333 31.6667 22.0000
and is incorrect

답변 (2개)

Walter Roberson
Walter Roberson 2023년 1월 19일

0 개 추천

that would be a row of 5 values inside a matrix that has 4 columns not 5. That is not possible
There are several functions for creating regularly spaced vectors of given lengths depending on which three of the four parameters (start point, end point, increment, and number of points) you know.
  • For start point, end point, increment use colon.
startPoint = 1;
endPoint = 10;
increment = 0.5;
numPoints = 19;
x1 = startPoint:increment:endPoint
x1 = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
  • For start point, end point, number of points use linspace.
x2 = linspace(startPoint, endPoint, numPoints)
x2 = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
  • For start point, increment, number of points use colon.
x3 = startPoint + increment*(0:numPoints-1)
x3 = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
  • For end point, increment, number of points use colon. This one essentially flips the sign of the increment and the vector from the start point, increment, number of points case. Or you could negate the sign of the increment, use the (start point, increment, number of points) case, then flip the resulting vector.
x4 = endPoint + increment*((1-numPoints):0)
x4 = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
startPoint2 = endPoint;
x5 = flip(startPoint2 + (-increment)*(0:numPoints-1))
x5 = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
Are they all the same?
[isequal(x1, x2), isequal(x2, x3), isequal(x3, x4), isequal(x4, x5)]
ans = 1×4 logical array
1 1 1 1

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2023년 1월 19일

답변:

2023년 1월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by