필터 지우기
필터 지우기

how to make the same length in the struct

조회 수: 3 (최근 30일)
Luca Re
Luca Re 2023년 8월 28일
댓글: Luca Re 2023년 8월 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)
Luca Re
Luca Re 2023년 8월 30일
thank you

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

추가 답변 (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