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

조회 수: 1 (최근 30일)
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

채택된 답변

Chuguang Pan
Chuguang Pan 2025년 6월 21일
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
VBBV
VBBV 2025년 6월 21일
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 2025년 6월 21일
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>

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by