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
댓글 수: 0
채택된 답변
Chuguang Pan
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)
댓글 수: 3
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
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
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
C = {[1 2], 3, 4:6}
Determine the maximum length of the data stored in cells in C.
lengthOfVectorsInCell = cellfun(@numel, C)
maxLength = max(lengthOfVectorsInCell)
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)
Now that the vectors in the cell array are the same length, they can be concatenated.
D = vertcat(paddedC{:})
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!