Erorr when replace row's value of table in-array.

1 Ansicht (letzte 30 Tage)
Arif
Arif am 7 Mai 2024
Beantwortet: Voss am 7 Mai 2024
hi guys, i dont know why this error happened to me. I already check the variable and i think it is already rigth.
Here is my code :
clear;
clc;
% Model Parameter
H = 5;
L = 15;
Ns = 2;
Nb = 1;
PortalHeight = H;
Bayspan = L;
NumberStory = Ns;
NumberBay = Nb;
% Variable1
h = [0.8,1.00,1.20,1.40,1.6,1.80];
% Variable2
Lss = [2.00,2.40,2.80,3.20,3.60,4.00];
% Variable3
Nss = [2,3,4];
% Definition variant
Var1HeightofTrussBeam = h;
Var2LengthSpecialSegment = Lss;
Var3NumberSpecialSegment = Nss;
ijk = 0;
for i = 1:numel(h)
for j = 1:numel(Lss)
for k = 1:numel(Nss)
ijk = ijk+1;
Variantmodel{ijk,:} = table(ijk,h(i),Lss(j),Nss(k),'VariableNames',{'VariantID','Height','Length','Number'});
%% Cooridinate Point Base(base)
Xbase{ijk,:} = L*(0:NumberBay);
Zbase{ijk,:} = repelem(0,1,length(Xbase{ijk,:}));
%% Column(col)
Xcolumn = L*(0:NumberBay);
Zcolumn = repelem(H*(1:NumberStory),1,length(Xcolumn));
Xbotbeam = L*(0:NumberBay);
Zbotbeam = repelem((H*(1:NumberStory)-h(i)),1,length(Xbotbeam));
idx = unique(Zbotbeam(:).');
Zbottombeam{ijk,:} = idx;
% replicate [X] to be the same size as [Z] for plotting:
Xcol{ijk,:} = repmat([Xcolumn,Xbotbeam],1,NumberStory);
Zcol{ijk,:} = [Zcolumn,Zbotbeam];
%% Cooridinate Point Ordinary Segment (os)
% calculate span length of ordinary segment
Los = (L-Lss(j))/2;
Nos = Los/h(i);
Nos = round(Nos);
numberordinarysegment{ijk,:} = Nos;
los = Los/Nos;
totalspan{ijk,:} = los*Nos*2+Lss(j);
if totalspan{ijk,:} == 15.000
checkspan{ijk,:} = "correct span";
else
checkspan{ijk,:} = "span must be 15m";
end
% top and bottom joint of ordinary segment(os)
XTopOrdinarySegment = reshape([los*(1:Nos),(L/2+Lss(j)/2)+los*(0:Nos-1)].'+L*(0:(NumberBay-1)),1,[]);
XBottomOrdinarySegment = XTopOrdinarySegment;
ZTopOrdinarySegment = repelem(H*(1:NumberStory),1,2*Nos*NumberBay);
ZBottomOrdinarySegment = repelem(H*(1:NumberStory)-h(i),1,2*Nos*NumberBay);
% replicate [X] to be the same size as [Z] for plotting:
Xos{ijk,:} = repmat([XTopOrdinarySegment,XBottomOrdinarySegment],1,NumberStory);
Zos{ijk,:} = [ZTopOrdinarySegment,ZBottomOrdinarySegment];
%% Cooridinate Point Special Segment (ss)
% calculate span length of special segment
lss = Lss(j)/Nss(k);
% top and bottom joint of special segment(ss)
XTopSpecialSegment = reshape((lss*(0:Nss(k))+L/2-Lss(j)/2).'+L*(0:(NumberBay-1)),1,[]);
XBottomSpecialSegment = XTopSpecialSegment;
ZTopSpecialSegment = repelem(H*(1:NumberStory),1,length(XTopSpecialSegment));
ZBottomSpecialSegment = repelem(H*(1:NumberStory)-h(i),1,length(XBottomSpecialSegment));
% replicate [X] to be the same size as [Z] for plotting:
Xss{ijk,:} = repmat([XTopSpecialSegment,XBottomSpecialSegment],1,NumberStory);
Zss{ijk,:} = [ZTopSpecialSegment,ZBottomSpecialSegment];
%% Plot Cooridinate Point (X,Z)
X{ijk,:} = [Xbase{ijk,:},Xcol{ijk,:},Xos{ijk,:},Xss{ijk,:}];
Z{ijk,:} = [Zbase{ijk,:},Zcol{ijk,:},Zos{ijk,:},Zss{ijk,:}];
scatter(X{ijk,:},Z{ijk,:},'filled'),grid on
%check angle
tangent = (Lss(j)/Nss(k))/h(i);
anglespecialsegment(ijk) = atand(tangent);
%% Name the Joint
jointbase{ijk} = table((1:numel(Xbase{ijk,:}))',Xbase{ijk,:}',Zbase{ijk,:}','VariableNames',{'ID','X','Z'});
jointcolumn{ijk} = table((max(jointbase{ijk}.ID)+1:numel(Xcol{ijk,:})+max(jointbase{ijk}.ID))',Xcol{ijk,:}',Zcol{ijk,:}','VariableNames',{'ID','X','Z'});
jointordinarysegment{ijk} = table((max(jointcolumn{ijk}.ID)+1:numel(Xos{ijk,:})+max(jointcolumn{ijk}.ID))',Xos{ijk,:}',Zos{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zcolumn),:);
jointbottomordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zbotbeam),:);
jointspecialsegment{ijk} = table((max(jointbottomordinarysegment{ijk}.ID)+1:numel(Xss{ijk,:})+max(jointbottomordinarysegment{ijk}.ID))',Xss{ijk,:}',Zss{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zcolumn),:);
jointbottomspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zbotbeam),:);
end
end
end
% Verify Variants Which Passed the Angle Requirement
tablecheck = table((1:numel(anglespecialsegment))',totalspan,anglespecialsegment','VariableNames',{'VariantID','Total Span','Angle'});
VariantIDPassed = tablecheck.VariantID(tablecheck.Angle>=30 & tablecheck.Angle<=60);
jointbase = table((1:numel(jointbase))',jointbase','VariableNames',{'VariantID','Joint Base'});
jointbase = jointbase(ismember(jointbase.VariantID,VariantIDPassed),:);
jointcolumn = table((1:numel(jointcolumn))',jointcolumn','VariableNames',{'VariantID','Joint Column'});
jointcolumn = jointcolumn(ismember(jointcolumn.VariantID,VariantIDPassed),:);
jointtopordinarysegment = table((1:numel(jointtopordinarysegment))',jointtopordinarysegment','VariableNames',{'VariantID','Joint Top Ordinary Segment'});
jointtopordinarysegment = jointtopordinarysegment(ismember(jointtopordinarysegment.VariantID,VariantIDPassed),:);
jointbottomordinarysegment = table((1:numel(jointbottomordinarysegment))',jointbottomordinarysegment','VariableNames',{'VariantID','Joint Bottom Ordinary Segment'});
jointbottomordinarysegment = jointbottomordinarysegment(ismember(jointbottomordinarysegment.VariantID,VariantIDPassed),:);
jointtopspecialsegment = table((1:numel(jointtopspecialsegment))',jointtopspecialsegment','VariableNames',{'VariantID','Joint Top Special Segment'});
jointtopspecialsegment = jointtopspecialsegment(ismember(jointtopspecialsegment.VariantID,VariantIDPassed),:);
jointbottomspecialsegment = table((1:numel(jointbottomspecialsegment))',jointbottomspecialsegment','VariableNames',{'VariantID','Joint Bottom Special Segment'});
jointbottomspecialsegment = jointbottomspecialsegment(ismember(jointbottomspecialsegment.VariantID,VariantIDPassed),:);
Zbottombeam{ijk} = table((1:numel(Zbottombeam{ijk,:}))',Zbottombeam{ijk,:}','VariableNames',{'VariantID','Z Bottom Beam'});
Zbottombeam = Zbottombeam(VariantIDPassed);
%% Name the Frame
% column
for i = 1:height(VariantIDPassed)
jointIcolumn = 0;
jointJcolumn = 0;
jointcolumnnew{i,:} = jointcolumn.("Joint Column"){i}(ismember(jointcolumn.("Joint Column"){i}.Z,jointtopordinarysegment.("Joint Top Ordinary Segment"){i}.Z),:);
jointcolumnnew{i,:} = vertcat(jointbase.("Joint Base"){i},jointcolumnnew{i});
for j = 1:height(jointcolumnnew{i,1})-2
jointIcolumn(j) = jointcolumnnew{i,1}.ID(j);
jointJcolumn(j) = jointcolumnnew{i,1}.ID(j+2);
end
framecolumn{i,:} = table((1:j)',jointIcolumn',jointJcolumn','VariableNames',{'Frame ID','JointI','JointJ'});
end
%% Name the Frame
% top ordinary segment (TOS) (this phase is not automatic for different story)
for i = 1:height(VariantIDPassed)
endTOS{i,:} = jointcolumn.("Joint Column"){i}(ismember(jointcolumn.("Joint Column"){i}.Z,jointtopordinarysegment.("Joint Top Ordinary Segment"){i}.Z),:);
jointTOS{i,:} = vertcat(jointtopordinarysegment.("Joint Top Ordinary Segment"){i},endTOS{i});
jointTOSnew1{i,:} = sortrows(jointTOS{i}(ismember(jointTOS{i}.Z,H),:),2);
jointTOSnew2{i,:} = sortrows(jointTOS{i}(ismember(jointTOS{i}.Z,H*Ns),:),2);
jointTOSnew{i,:} = vertcat(jointTOSnew1{i},jointTOSnew2{i});
end
for i = 1:height(VariantIDPassed)
jointI = 0;
jointJ = 0;
for j = 1:height(jointTOSnew1{i,1})-1
jointI(j) = jointTOSnew1{i,1}.ID(j);
jointJ(j) = jointTOSnew1{i,1}.ID(j+1);
end
lastframenumber{i,:} = max(framecolumn{i,1}.("Frame ID"));
frameTOS1{i,:} = table((lastframenumber{i}+1:lastframenumber{i}+j)',jointI',jointJ','VariableNames',{'Frame ID','JointI','JointJ'});
%take special segment
frameTSB1{i,:} = frameTOS1{i,1}(median(1:height(frameTOS1{i,1})),:);
%eliminate special segment
frameTOS1{i,1}(median(1:height(frameTOS1{i,1})),:) = [];
%specified end and mid
endsegment1 = 1:round(height(frameTOS1{i,1})/4);
endsegment2 = (1+height(frameTOS1{i,1})-round(height(frameTOS1{i,1})/4)):height(frameTOS1{i,1});
endsegment = horzcat(endsegment1,endsegment2);
midsegment = not(ismember(1:height(frameTOS1{i,1}),endsegment));
frameTOSend1{i,:} = frameTOS1{i,1}(endsegment,:);
frameTOSmid1{i,:} = frameTOS1{i,1}(midsegment,:);
end
for i = 1:height(VariantIDPassed)
jointI = 0;
jointJ = 0;
for j = 1:height(jointTOSnew2{i,1})-1
jointI(j) = jointTOSnew2{i,1}.ID(j);
jointJ(j) = jointTOSnew2{i,1}.ID(j+1);
end
lastframenumber{i,:} = max(frameTOS1{i,1}.("Frame ID"));
frameTOS2{i,:} = table((lastframenumber{i}+1:lastframenumber{i}+j)',jointI',jointJ','VariableNames',{'Frame ID','JointI','JointJ'});
%eliminate special segment
frameTOS2{i,1}(median(1:height(frameTOS2{i,1})),:) = [];
%specified end and mid
endsegment1 = 1:round(height(frameTOS2{i,1})/4);
endsegment2 = (1+height(frameTOS2{i,1})-round(height(frameTOS2{i,1})/4)):height(frameTOS2{i,1});
endsegment = horzcat(endsegment1,endsegment2);
midsegment = not(ismember(1:height(frameTOS2{i,1}),endsegment));
frameTOSend2{i,:} = frameTOS2{i,1}(endsegment,:);
frameTOSmid2{i,:} = frameTOS2{i,1}(midsegment,:);
end
frameTOS = cellfun(@(varargin)vertcat(varargin{:}),frameTOS1,frameTOS2,'UniformOutput',false);
frameTOSend = cellfun(@(varargin)vertcat(varargin{:}),frameTOSend1,frameTOSend2,'UniformOutput',false);
frameTOSmid = cellfun(@(varargin)vertcat(varargin{:}),frameTOSmid1,frameTOSmid2,'UniformOutput',false);
%% Name the Frame
% bottom ordinary segment (BOS) (this phase is not automatic for different story)
for i = 1:height(VariantIDPassed)
endBOS{i,:} = jointcolumn.("Joint Column"){i}(ismember(jointcolumn.("Joint Column"){i}.Z,jointbottomordinarysegment.("Joint Bottom Ordinary Segment"){i}.Z),:);
jointBOS{i,:} = vertcat(jointbottomordinarysegment.("Joint Bottom Ordinary Segment"){i},endBOS{i});
jointBOSnew1{i,:} = sortrows(jointBOS{i}(ismember(jointBOS{i}.Z,Zbottombeam{i}(1)),:),2);
jointBOSnew2{i,:} = sortrows(jointBOS{i}(ismember(jointBOS{i}.Z,Zbottombeam{i}(2)),:),2);
jointBOSnew{i,:} = vertcat(jointBOSnew1{i},jointBOSnew2{i});
end
for i = 1:height(VariantIDPassed)
jointI = 0;
jointJ = 0;
for j = 1:height(jointBOSnew1{i,1})-1
jointI(j) = jointBOSnew1{i,1}.ID(j);
jointJ(j) = jointBOSnew1{i,1}.ID(j+1);
end
lastframenumber{i,:} = max(frameTOS{i,1}.("Frame ID"));
frameBOS1{i,:} = table((lastframenumber{i}+1:lastframenumber{i}+j)',jointI',jointJ','VariableNames',{'Frame ID','JointI','JointJ'});
%take special segment
frameBSB1{i,:} = frameBOS1{i,1}(median(1:height(frameBOS1{i,1})),:);
%eliminate special segment
frameBOS1{i,1}(median(1:height(frameBOS1{i,1})),:) = [];
%specified end and mid
endsegment1 = 1:round(height(frameBOS1{i,1})/4);
endsegment2 = (1+height(frameBOS1{i,1})-round(height(frameBOS1{i,1})/4)):height(frameBOS1{i,1});
endsegment = horzcat(endsegment1,endsegment2);
midsegment = not(ismember(1:height(frameBOS1{i,1}),endsegment));
frameBOSend1{i,:} = frameBOS1{i,1}(endsegment,:);
frameBOSmid1{i,:} = frameBOS1{i,1}(midsegment,:);
end
for i = 1:height(VariantIDPassed)
jointI = 0;
jointJ =0;
for j = 1:height(jointBOSnew2{i,1})-1
jointI(j) = jointBOSnew2{i,1}.ID(j);
jointJ(j) = jointBOSnew2{i,1}.ID(j+1);
end
lastframenumber{i,:} = max(frameBOS1{i,1}.("Frame ID"));
frameBOS2{i,:} = table((lastframenumber{i}+1:lastframenumber{i}+j)',jointI',jointJ','VariableNames',{'Frame ID','JointI','JointJ'});
%eliminate special segment
frameBOS2{i,1}(median(1:height(frameBOS2{i,1})),:) = [];
%specified end and mid
endsegment1 = 1:round(height(frameBOS2{i,1})/4);
endsegment2 = (1+height(frameBOS2{i,1})-round(height(frameBOS2{i,1})/4)):height(frameBOS2{i,1});
endsegment = horzcat(endsegment1,endsegment2);
midsegment = not(ismember(1:height(frameBOS2{i,1}),endsegment));
frameBOSend2{i,:} = frameBOS2{i,1}(endsegment,:);
frameBOSmid2{i,:} = frameBOS2{i,1}(midsegment,:);
end
frameBOS = cellfun(@(varargin)vertcat(varargin{:}),frameBOS1,frameBOS2,'UniformOutput',false);
frameBOSend = cellfun(@(varargin)vertcat(varargin{:}),frameBOSend1,frameBOSend2,'UniformOutput',false);
frameBOSmid = cellfun(@(varargin)vertcat(varargin{:}),frameBOSmid1,frameBOSmid2,'UniformOutput',false);
%% Name the Frame
% top special segment (TSS) (this phase is not automatic for different story)
for i = 1:height(VariantIDPassed)
jointTSS1{i,:} = sortrows(jointtopspecialsegment.("Joint Top Special Segment"){i,1}(ismember(jointtopspecialsegment.("Joint Top Special Segment"){i,1}.Z,H),:),2);
jointTSS2{i,:} = sortrows(jointtopspecialsegment.("Joint Top Special Segment"){i,1}(ismember(jointtopspecialsegment.("Joint Top Special Segment"){i,1}.Z,H*Ns),:),2);
%jointTSSnew{i,:} = vertcat(jointTSSnew1{i},jointTSSnew2{i});
startTSS1{i,:} = jointTSS1{i,1}.X(1);
endTSS1{i,:} = jointTSS1{i,1}.X(height(jointTSS1{i,1}));
jointTSS1{i,1}.ID(1) = jointTOSnew1{i,1}.ID(ismember(jointTOSnew1{i,1}.X,startTSS1{i,1}));
end
Error using . (line 507)
Unable to perform assignment because the left and right sides have a different number of elements.

Akzeptierte Antwort

Voss
Voss am 7 Mai 2024
Sometimes the X don't exactly match (e.g., they're different by ~1e-15), so this finds the closest.
for ii = 1:numel(jointTSS1)
% index of the closest X in jointTOSnew1{ii} to the first and last X in jointTSS1{ii}
[~,idx] = min( abs( jointTSS1{ii}.X([1 end]).' - jointTOSnew1{ii}.X ), [] , 1)
% replace the first and last ID in jointTSS1{ii} with the ID in jointTOSnew1{ii} at the closest X
jointTSS1{ii}.ID([1 end]) = jointTOSnew1{ii}.ID(idx);
end

Weitere Antworten (1)

Steven Lord
Steven Lord am 7 Mai 2024
% jointTSS1{i,1}.ID(1) = jointTOSnew1{i,1}.ID(ismember(jointTOSnew1{i,1}.X,startTSS1{i,1}));
You're assuming that the ismember call returns an array with exactly 1 true element. What happens if that's not the case?
a = 1:4;
a(1) = a([false true false false]) % works, exactly 1 true element
a = 1x4
2 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
a(1) = a([false false true true]) % does not work, you can't fit two elements into one
Unable to perform assignment because the left and right sides have a different number of elements.
Note that you need to worry about both the case where the output of ismember contains multiple true elements and the case where it contains no true elements. The following line of code would have thrown the same error as the line with 2 true elements above, had it been executed.
a(1) = a([false false false false])
  1 Kommentar
Arif
Arif am 7 Mai 2024
i dont get it, look my picture below. As you can see, I dont have two elements in the table which have same value...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by