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
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
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));
카테고리
도움말 센터 및 File Exchange에서 Image Category Classification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!