using linspace in a matrix incrementing by a certain number

37 visualizzazioni (ultimi 30 giorni)
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

Risposte (2)

Walter Roberson
Walter Roberson il 19 Gen 2023
that would be a row of 5 values inside a matrix that has 4 columns not 5. That is not possible

Steven Lord
Steven Lord il 19 Gen 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
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

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by