I am trying to create a character vector using a for loop?

2 views (last 30 days)
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

Answers (2)

KSSV
KSSV on 8 May 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
Ameer Hamza on 8 May 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));

Community Treasure Hunt

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

Start Hunting!