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

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

Respuestas (2)

KSSV
KSSV el 8 de Mayo de 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
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));

Preguntada:

el 8 de Mayo de 2018

Respondida:

el 8 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by