I am trying to create a character vector using a for loop?
Mostra commenti meno recenti
I am trying to create a character vector using a for loop with a couple of if statements and my out out is coming out with NAN currently.
A= Anodes;
x= extforce';
f1= A\x;
F= inv(A)*x;
F=round(F,6);
ten= "Tension";
comp= "Compression";
zfm= "ZeroForceMember";
Type= [];
for i= 1:length(F)
if F(i,:)> 0
Type(i,:)= ten
elseif F(i,:)< 0
Type(i,:)= comp
else
Type(i,:)= zfm
end
end
Type
Mem= ["QA","AB","BC","CD","DE","EF","FP","GH","HJ","JK","KL","LM","GA","HB","JC","KD","LE","MF","GB","HC","JD","DL","ME","GQ","MP","QX","QY","PY"]';
table(Mem,F)
>> matlabproj12
Type =
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
Risposte (2)
KSSV
il 8 Mag 2018
A= Anodes;
x= extforce';
f1= A\x;
F= inv(A)*x;
F=round(F,6);
ten= "Tension";
comp= "Compression";
zfm= "ZeroForceMember";
Type= cell(length(F),1) ;
for i= 1:length(F)
if F(i,:)> 0
Type{i}= ten
elseif F(i,:)< 0
Type{i}= comp
else
Type{i}= zfm
end
end
Ameer Hamza
il 8 Mag 2018
by initializing using
Type= [];
you are specifying it type as double. So by just removing this line, the code will work fine.
If you care about preallocation you can do this
Type= string(zeros(length(F),1));
Categorie
Scopri di più su Matrices and Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!