How to have a vector that is obtained by discretizing a and b for each i.

1 visualizzazione (ultimi 30 giorni)
For example, when i=1, a(1)=1, b(1)=24; c=1 0.5 2 2.5 up to 24. Then we move on to i=2

Risposta accettata

Chuguang Pan
Chuguang Pan il 21 Giu 2025
you can use arrayfun function to process array element.
a=[1 2 3 4 5 6 7 8 9 10 11 12];
b=[24 23 22 21 20 19 18 17 16 15 14 13];
stepHalfFun = @(start,stop) start:.5:stop;
res = arrayfun(stepHalfFun,a,b,'UniformOutput',false)
res = 1×12 cell array
Columns 1 through 10 {1×47 double} {1×43 double} {1×39 double} {1×35 double} {1×31 double} {1×27 double} {1×23 double} {1×19 double} {1×15 double} {1×11 double} Columns 11 through 12 {1×7 double} {[12 12.5000 13]}
  3 Commenti
VBBV
VBBV il 21 Giu 2025
a=[1 2 3 4 5 6 7 8 9 10 11 12];
b=[24 23 22 21 20 19 18 17 16 15 14 13];
stepHalfFun = @(start,stop) start:.5:stop;
res = arrayfun(stepHalfFun,a,b,'UniformOutput',false).' % transpose
res = 12×1 cell array
{[ 1 1.5000 2 2.5000 3 3.5000 4 4.5000 5 5.5000 6 6.5000 7 7.5000 8 8.5000 9 9.5000 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15 15.5000 16 … ] (1×47 double)} {[ 2 2.5000 3 3.5000 4 4.5000 5 5.5000 6 6.5000 7 7.5000 8 8.5000 9 9.5000 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15 15.5000 16 16.5000 17 … ] (1×43 double)} {[ 3 3.5000 4 4.5000 5 5.5000 6 6.5000 7 7.5000 8 8.5000 9 9.5000 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15 15.5000 16 16.5000 17 17.5000 18 … ] (1×39 double)} {[4 4.5000 5 5.5000 6 6.5000 7 7.5000 8 8.5000 9 9.5000 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15 15.5000 16 16.5000 17 17.5000 18 18.5000 19 … ] (1×35 double)} {[ 5 5.5000 6 6.5000 7 7.5000 8 8.5000 9 9.5000 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15 15.5000 16 16.5000 17 17.5000 18 18.5000 19 19.5000 20]} {[ 6 6.5000 7 7.5000 8 8.5000 9 9.5000 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15 15.5000 16 16.5000 17 17.5000 18 18.5000 19]} {[ 7 7.5000 8 8.5000 9 9.5000 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15 15.5000 16 16.5000 17 17.5000 18]} {[ 8 8.5000 9 9.5000 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15 15.5000 16 16.5000 17]} {[ 9 9.5000 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15 15.5000 16]} {[ 10 10.5000 11 11.5000 12 12.5000 13 13.5000 14 14.5000 15]} {[ 11 11.5000 12 12.5000 13 13.5000 14]} {[ 12 12.5000 13]}
Steven Lord
Steven Lord il 21 Giu 2025
Another question how to transform it in matrix with 12 line ?.
In MATLAB, arrays in MATLAB cannot be "jagged" -- all the rows have to have the same number of columns. [There is an exception for arrays of Java objects under certain circumstances, I think.] So you can't have a matrix where one row has 2 elements and one has 1 element. You could pad it with NaN values or find some other padding value.
A = [1 2; 3 NaN] % works
A = 2×2
1 2 3 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note that without the NaN, this wouldn't work. I left this commented out so I could run the rest of the code in this answer.
% B = [1 2; 3] % would not work
One way to pad the data would be with paddata. Let's take some sample data.
C = {[1 2], 3, 4:6}
C = 1×3 cell array
{[1 2]} {[3]} {[4 5 6]}
Determine the maximum length of the data stored in cells in C.
lengthOfVectorsInCell = cellfun(@numel, C)
lengthOfVectorsInCell = 1×3
2 1 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
maxLength = max(lengthOfVectorsInCell)
maxLength = 3
Create the padding function that will pad each cell to that maximum length, filling in the newly added elements with NaN.
paddingFunction = @(x) paddata(x, maxLength, FillValue = NaN);
Apply the padding function to the cell array.
paddedC = cellfun(paddingFunction, C, UniformOutput = false)
paddedC = 1×3 cell array
{[1 2 NaN]} {[3 NaN NaN]} {[4 5 6]}
Now that the vectors in the cell array are the same length, they can be concatenated.
D = vertcat(paddedC{:})
D = 3×3
1 2 NaN 3 NaN NaN 4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Accedi per commentare.

Più risposte (0)

Categorie

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

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by