using linspace in a matrix incrementing by a certain number
31 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Walter Roberson
on 19 Jan 2023
that would be a row of 5 values inside a matrix that has 4 columns not 5. That is not possible
0 Comments
Steven Lord
on 19 Jan 2023
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)]
0 Comments
See Also
Categories
Find more on Matrices and Arrays 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!