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

답변 (2개)

KSSV
KSSV 2018년 5월 8일
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 2018년 5월 8일
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));

질문:

2018년 5월 8일

답변:

2018년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by