how to make the same length in the struct

3 次查看(过去 30 天)
i've a struct with filed of various length (see pics)
i want to make the same length of this array
and assign the zero in the data that is not there at the end
i try this ..but it's not correct
>> [Sis.d]
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
  8 个评论
Bruno Luong
Bruno Luong 2023-8-29
编辑:Bruno Luong 2023-8-29
This thread becomes completely a mess. The problem asked now has nothing to do with Padding arrays in structure.
The other thread should not be closed.
Luca Re
Luca Re 2023-8-29
编辑:Luca Re 2023-8-29
using last example i write code to do it:
%DATA "XX1"
data=[
'25-Feb-2008'
'26-Feb-2008'
'27-Feb-2008'
];
data=datetime(data);
%DAILYPROF "XX1"
dailyprof=[
4
5
6
];
b(1)=struct("Sistema",{"xx1"},"Date",data,"dailyprof",dailyprof);
%DATA "XX21"
data=[
'26-Feb-2008'
'27-Feb-2008'
'28-Feb-2008'
];
data=datetime(data);
%DAILYPROF "XX2"
dailyprof=[
7
8
9
];
b(2)=struct("Sistema",{"xx2"},"Date",data,"dailyprof",dailyprof);
%{
25-feb-2023 4 0
26-feb-2023 5 7
27-feb-2023 6 8
28-feb-2023 0 9
%}
v=[b.Date];
g=unique(v)
hh=zeros(size(g));
for i=1:numel(g)
for h=1:numel(b(1).Date)
if g(i)==b(1).Date(h)
hh(i)=b(1).dailyprof(h);
end
end
end
M(1)=struct("Sistema",{"xx1"},"Date",g,"dailyprof",hh);
hh=zeros(size(g));
for i=1:numel(g)
for h=1:numel(b(2).Date)
if g(i)==b(2).Date(h)
hh(i)=b(2).dailyprof(h);
end
end
end
M(2)=struct("Sistema",{"xx2"},"Date",g,"dailyprof",hh);
RESULT IS M!

请先登录,再进行评论。

采纳的回答

Bruno Luong
Bruno Luong 2023-8-29
load('matlab_sis.mat')
Date = unique(cat(1,Sis.Date));
DAILYPROOF = nan(length(Date),length(Sis));
for k=1:length(Sis)
sk = Sis(k);
[tf, i] = ismember(sk.Date, Date);
DAILYPROOF(i,k) = sk.dailyprof;
end
T = table(Date, DAILYPROOF)
T = 71×2 table
Date DAILYPROOF ___________ _______________________ 24-Feb-2008 1510 NaN NaN 25-Feb-2008 0 NaN NaN 26-Feb-2008 940 NaN NaN 27-Feb-2008 0 NaN NaN 28-Feb-2008 0 NaN NaN 29-Feb-2008 0 NaN NaN 01-Mar-2008 0 NaN NaN 02-Mar-2008 -70 NaN NaN 03-Mar-2008 -190 0 NaN 04-Mar-2008 -1360 0 NaN 05-Mar-2008 0 290 NaN 06-Mar-2008 0 1220 NaN 07-Mar-2008 0 0 NaN 08-Mar-2008 0 120 NaN 09-Mar-2008 180 0 NaN 10-Mar-2008 -140 0 NaN
  3 个评论
Bruno Luong
Bruno Luong 2023-8-29
Version without loop
n = length(Sis);
Date = {Sis.Date};
lgt = cellfun(@length, Date);
[Date, ~, i] = unique(cat(1,Date{:}));
j = repelem((1:n)',lgt(:));
DAILYPROOF = nan(length(Date),length(Sis));
DAILYPROOF(i+n*(j-1)) = cat(1,Sis.dailyprof);
T = table(Date, DAILYPROOF)

请先登录,再进行评论。

更多回答(1 个)

Bruno Luong
Bruno Luong 2023-8-28
s=struct('a',rand(5,1),'b',rand(3,1),'c',rand(6,1))
s = struct with fields:
a: [5×1 double] b: [3×1 double] c: [6×1 double]
maxheight = max(structfun(@height,s));
padarrays = structfun(@(x) [x; zeros(maxheight-height(x),1)], s, 'unif', 0)
padarrays = struct with fields:
a: [6×1 double] b: [6×1 double] c: [6×1 double]
padarrays.a
ans = 6×1
0.8804 0.0268 0.8319 0.5125 0.7417 0
padarrays.b
ans = 6×1
0.5513 0.1335 0.7522 0 0 0
padarrays.c
ans = 6×1
0.4471 0.9683 0.7315 0.4339 0.3527 0.5825
  1 个评论
Luca Re
Luca Re 2023-8-28
编辑:Luca Re 2023-8-28
i get this error4:
s=Sis.d;
Error using structfun
Inputs to STRUCTFUN must be scalar structures.
Error in untitled2 (line 6)
maxheight = max(structfun(@height,s));
because in the structure there are other camps besides the Sis.d

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by