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
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
- For start point, end point, number of points use linspace.
x2 = linspace(startPoint, endPoint, numPoints)
- For start point, increment, number of points use colon.
x3 = startPoint + increment*(0:numPoints-1)
- 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)
startPoint2 = endPoint;
x5 = flip(startPoint2 + (-increment)*(0:numPoints-1))
Are they all the same?
[isequal(x1, x2), isequal(x2, x3), isequal(x3, x4), isequal(x4, x5)]
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!