Extracting data from struct as matrix

조회 수: 13 (최근 30일)
Rashi Monga
Rashi Monga 2024년 6월 13일
댓글: Stephen23 2024년 6월 14일
Hi,
I have the following struct:
S(1).a = 1:5;
S(2).a = 11:10;
S(3).a = 21:27;
Is there a way to vertically concatenate them without looping? I cannot do vertcat(S.a) because data is of different size.
I want output like this [1:5; 11:10; 21:27]
Thank you
  댓글 수: 2
Ganesh
Ganesh 2024년 6월 13일
A matrix with these dimensions cannot be constructed.
a = [1:5; 11:10; 21:27]
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Stephen23
Stephen23 2024년 6월 13일
"I want output like this [1:5; 11:10; 21:27]"
Matrices cannot have rows of different lengths.

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

채택된 답변

Ganesh
Ganesh 2024년 6월 13일
You can consider the following workaround for your problem, without using for loops:
S(1).a = 1:10;
S(2).a = 11:17;
S(3).a = 21:26;
aCell = arrayfun(@(x) {x.a}, S);
% Find the maximum length among all arrays
maxLength = max(cellfun(@numel, aCell));
% Function to pad arrays with NaN to match the maxLength
padFunction = @(x) [x, NaN(1, maxLength - numel(x))];
y = cellfun(padFunction, aCell, 'UniformOutput', false);
y = vertcat(y{:})
y = 3x10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 NaN NaN NaN 21 22 23 24 25 26 NaN NaN NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  댓글 수: 2
Rashi Monga
Rashi Monga 2024년 6월 13일
Thanks everyone for their replies. It was helpful.
Stephen23
Stephen23 2024년 6월 14일
Alternatively doanload PADCAT here:
and use it like this:
S(1).a = 1:5;
S(2).a = 10:11;
S(3).a = 21:27;
M = padcat(S.a)
M = 3x7
1 2 3 4 5 NaN NaN 10 11 NaN NaN NaN NaN NaN 21 22 23 24 25 26 27
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

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

추가 답변 (2개)

Jonas
Jonas 2024년 6월 13일
편집: Jonas 2024년 6월 13일
as you already said, you canot concatenate the data as matrix, since it has different size. you could use a cell array, but this would be no better than the struct. another possibility coul be to pad shorter variables with NaN. but here, you would need a loop.
S(1).a = 1:5;
S(2).a = 11:10;
S(3).a = 21:27;
sizes=arrayfun(@(in) length(in.a),S);
maxSize=max(sizes);
asMatrix=nan(numel(sizes),maxSize);
for sNr=1:numel(S)
asMatrix(sNr,1:sizes(sNr))=S(sNr).a;
end
asMatrix
asMatrix = 3x7
1 2 3 4 5 NaN NaN NaN NaN NaN NaN NaN NaN NaN 21 22 23 24 25 26 27
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Shivani
Shivani 2024년 6월 13일
편집: Shivani 2024년 6월 13일
You will need to modify the solution in the above answer as seen in the below code snippet to obtain the desired result.
S(1).a = 1:5;
S(2).a = 11:15;
S(3).a = 21:25;
M = vertcat(S.a)
M = 3x5
1 2 3 4 5 11 12 13 14 15 21 22 23 24 25
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Please note that the ranges you have provided cannot be concatenated vertically because they will not be of the same length. I am therefore assuming the ranges to be 1:5, 11:15 and 21:25. Since they all contain 5 elements, we can concatenate them to a matrix containing 5 columns.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by