How to fix invalid array indexing ?

70 ビュー (過去 30 日間)
Arif
Arif 2024 年 4 月 12 日
編集済み: Cris LaPierre 2024 年 4 月 14 日
Hi guys im trying to colect all outputs tables that are created in-array. Here is my code :
clear;
clc;
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
Var1HeightofTrussBeam = h;
Var2LengthSpecialSegment = Lss;
Var3NumberSpecialSegment = Nss;
Totalvariant = zeros(numel(Var1HeightofTrussBeam)*numel(Var2LengthSpecialSegment)*numel(Var3NumberSpecialSegment),1);
Xbase = zeros(1,length(Totalvariant));
Zbase = zeros(1,length(Totalvariant));
Xcol = zeros(1,length(Totalvariant));
Zcol = zeros(1,length(Totalvariant));
Xos = zeros(1,length(Totalvariant));
Zos = zeros(1,length(Totalvariant));
Xss = zeros(1,length(Totalvariant));
Zss = zeros(1,length(Totalvariant));
X = zeros(1,length(Totalvariant));
Z = zeros(1,length(Totalvariant));
jointbase = zeros(1,length(Totalvariant));
jointcolumn = zeros(1,length(Totalvariant));
jointordinarysegment = zeros(1,length(Totalvariant));
jointtopordinarysegment = zeros(1,length(Totalvariant));
jointbottomordinarysegment = zeros(1,length(Totalvariant));
jointspecialsegment = zeros(1,length(Totalvariant));
jointtopspecialsegment = zeros(1,length(Totalvariant));
jointbottomspecialsegment = zeros(1,length(Totalvariant));
ijk = 0
for i = 1:numel(h)
for j = 1:numel(Lss)
for k = 1:numel(Nss)
ijk = ijk+1
%% Base(base)
Xbase(ijk) = L*(0:NumberBay);
Zbase(ijk) = repelem(0,1,length(Xbase));
%% 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));
% replicate [X] to be the same size as [Z] for plotting:
Xcol(ijk) = repmat([Xcolumn,Xbotbeam],1,NumberStory);
Zcol(ijk) = [Zcolumn,Zbotbeam];
%% Ordinary Segment
% calculate span length of ordinary segment
Los = (L-Lss(j))/2;
Nos = Los/h(i);
Nos = round(Nos);
los = Los/Nos;
assert(logical(los*Nos*2+Lss(i)==15),"span must be 15m");
% 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];
%% Special Segment
% 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 Joint
X(ijk) = [Xbase,Xcol,Xos,Xss];
Z(ijk) = [Zbase,Zcol,Zos,Zss];
scatter(X,Z,'filled'),grid on
%% Name Joint
jointbase(ijk) = table((1:numel(Xbase))',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),:);
Invalid array indexing.
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
%check angle
%anglespecialsegment = (Lss(1)/Nss(1))/h(1);
Error : Error: File: cobadoang4.m Line: 107
Column: 32
Invalid array indexing.
Here I give the example of table result one variant when h(1), Lss(1),Nss(1) :
  1 件のコメント
Arif
Arif 2024 年 4 月 12 日
here i give the script when h(1), Lss(1),Nss(1) :
clear;
clc;
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];
Var1HeightTrussBeam = h;
Var2LengthSpecialSegment = Lss;
Var3NumberSpecialSegment = Nss;
%% Base(base)
Xbase = L*(0:NumberBay);
Zbase = repelem(0,1,length(Xbase));
%% Column(col)
Xcolumn = L*(0:NumberBay);
Zcolumn = repelem(H*(1:NumberStory),1,length(Xcolumn));
Xbotbeam = L*(0:NumberBay);
Zbotbeam = repelem((H*(1:NumberStory)-h(1)),1,length(Xbotbeam));
% replicate [X] to be the same size as [Z] for plotting:
Xcol = repmat([Xcolumn,Xbotbeam],1,NumberStory);
Zcol = [Zcolumn,Zbotbeam];
%% Ordinary Segment
% calculate span length of ordinary segment
Los = (L-Lss(1))/2;
Nos = Los/h(1);
Nos = round(Nos);
los = Los/Nos;
assert(logical(los*Nos*2+Lss(1)==15),"span must be 15m");
% top and bottom joint of ordinary segment(os)
XTopOrdinarySegment = reshape([los*(1:Nos),(L/2+Lss(1)/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(1),1,2*Nos*NumberBay);
% replicate [X] to be the same size as [Z] for plotting:
Xos = repmat([XTopOrdinarySegment,XBottomOrdinarySegment],1,NumberStory);
Zos = [ZTopOrdinarySegment,ZBottomOrdinarySegment];
%% Special Segment
% calculate span length of special segment
lss = Lss(1)/Nss(1);
% top and bottom joint of special segment(ss)
XTopSpecialSegment = reshape((lss(1)*(0:Nss(1))+L/2-Lss(1)/2).'+L*(0:(NumberBay-1)),1,[]);
XBottomSpecialSegment = XTopSpecialSegment;
ZTopSpecialSegment = repelem(H*(1:NumberStory),1,length(XTopSpecialSegment));
ZBottomSpecialSegment = repelem(H*(1:NumberStory)-h(1),1,length(XBottomSpecialSegment));
% replicate [X] to be the same size as [Z] for plotting:
Xss = repmat([XTopSpecialSegment,XBottomSpecialSegment],1,NumberStory);
Zss = [ZTopSpecialSegment,ZBottomSpecialSegment];
%% Plot Joint
X = [Xbase,Xcol,Xos,Xss];
Z = [Zbase,Zcol,Zos,Zss];
scatter(X,Z,'filled'),grid on
%% Name Joint
jointbase = table((1:numel(Xbase))',Xbase',Zbase','VariableNames',{'ID','X','Z'});
jointcolumn = table((max(jointbase.ID)+1:numel(Xcol)+max(jointbase.ID))',Xcol',Zcol','VariableNames',{'ID','X','Z'});
jointordinarysegment = table((max(jointcolumn.ID)+1:numel(Xos)+max(jointcolumn.ID))',Xos',Zos','VariableNames',{'ID','X','Z'});
jointtopordinarysegment = jointordinarysegment(ismember(jointordinarysegment.Z,Zcolumn),:);
jointbottomordinarysegment = jointordinarysegment(ismember(jointordinarysegment.Z,Zbotbeam),:);
jointospecialsegment = table((max(jointbottomordinarysegment.ID)+1:numel(Xss)+max(jointbottomordinarysegment.ID))',Xss',Zss','VariableNames',{'ID','X','Z'});
jointtopspecialsegment = jointospecialsegment(ismember(jointospecialsegment.Z,Zcolumn),:);
jointbottomspecialsegment = jointospecialsegment(ismember(jointospecialsegment.Z,Zbotbeam),:);
%check angle
%anglespecialsegment = (Lss(1)/Nss(1))/h(1);

サインインしてコメントする。

採用された回答

Cris LaPierre
Cris LaPierre 2024 年 4 月 12 日
編集済み: Cris LaPierre 2024 年 4 月 14 日
This is not valid syntax
jointordinarysegment(ijk)(ismember(jointordinarysegment(ijk).Z,Zbotbeam),:);
It looks like you are trying to create an array of tables. You will have to create a cell array to do that. That means using curly braces. Then use curly braces when indexing into your cell array.
% Create a cell array of tables
jointordinarysegment{ijk} = {table((max(jointcolumn(ijk).ID)+1:numel(Xos(ijk))+max(jointcolumn(ijk).ID))',Xos(ijk)',Zos(ijk)','VariableNames',{'ID','X','Z'})};
% Indexing into your cell array
jointtopordinarysegment(ijk) = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zcolumn),:);
Note that you will immediately encounter new errors with yoru code, as you have a size mismatch in some of your other indexing operations (you can't assign a vector to a single element).
  2 件のコメント
Cris LaPierre
Cris LaPierre 2024 年 4 月 12 日
編集済み: Cris LaPierre 2024 年 4 月 14 日
Because the length of your vectors change in the loops, your best options are to either not capture the intermediate values in your loops, or to turn them into cell arrays.
You might also benefit from this page on indexing tables.
Here's an example that captures the intermediate values in cell arrays.
clear;
clc;
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
Var1HeightofTrussBeam = h;
Var2LengthSpecialSegment = Lss;
Var3NumberSpecialSegment = Nss;
Totalvariant = zeros(numel(Var1HeightofTrussBeam)*numel(Var2LengthSpecialSegment)*numel(Var3NumberSpecialSegment),1);
% Xbase = zeros(1,length(Totalvariant));
% Zbase = zeros(1,length(Totalvariant));
% Xcol = zeros(1,length(Totalvariant));
% Zcol = zeros(1,length(Totalvariant));
% Xos = zeros(1,length(Totalvariant));
% Zos = zeros(1,length(Totalvariant));
% Xss = zeros(1,length(Totalvariant));
% Zss = zeros(1,length(Totalvariant));
% X = zeros(1,length(Totalvariant));
% Z = zeros(1,length(Totalvariant));
% jointbase = zeros(1,length(Totalvariant));
% jointcolumn = zeros(1,length(Totalvariant));
% jointordinarysegment = zeros(1,length(Totalvariant));
% jointtopordinarysegment = zeros(1,length(Totalvariant));
% jointbottomordinarysegment = zeros(1,length(Totalvariant));
% jointspecialsegment = zeros(1,length(Totalvariant));
% jointtopspecialsegment = zeros(1,length(Totalvariant));
% jointbottomspecialsegment = zeros(1,length(Totalvariant));
ijk = 0
ijk = 0
for i = 1:numel(h)
for j = 1:numel(Lss)
for k = 1:numel(Nss)
ijk = ijk+1
%% 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));
% replicate [X] to be the same size as [Z] for plotting:
Xcol{ijk,:} = repmat([Xcolumn,Xbotbeam],1,NumberStory);
Zcol{ijk,:} = [Zcolumn,Zbotbeam];
%% Ordinary Segment
% calculate span length of ordinary segment
Los = (L-Lss(j))/2;
Nos = Los/h(i);
Nos = round(Nos);
los = Los/Nos;
assert(logical(los*Nos*2+Lss(i)==15),"span must be 15m");
% 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];
%% Special Segment
% 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 Joint
X{ijk,:} = [Xbase{ijk,:},Xcol{ijk,:},Xos{ijk,:},Xss{ijk,:}];
Z{ijk,:} = [Zbase{ijk,:},Zcol{ijk,:},Zos{ijk,:},Zss{ijk,:}];
figure
scatter(X{ijk,:},Z{ijk,:},'filled'),grid on
%% Name 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
ijk = 1
ijk = 2
ijk = 3
ijk = 4
Error using assert
span must be 15m
%check angle
%anglespecialsegment = (Lss(1)/Nss(1))/h(1);
Arif
Arif 2024 年 4 月 14 日
thank you so much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGenomics and Next Generation Sequencing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by