首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matlab-无法正确打印我的代码输出?-

Matlab-无法正确打印我的代码输出?-
EN

Stack Overflow用户
提问于 2017-11-06 02:32:27
回答 1查看 37关注 0票数 0

在完成练习时,我输入了以下代码。输出为3乘1矢量,由空速、角度和方向组成。空速和角是一个数字的形式,方向是一个字符的形式。

代码语言:javascript
复制
windspeed=[1 3];
groundspeed=[5 3];

north = [0,1];
south = [0,-1];
east = [1,0];
west = [-1,0];
airspeed = 0;
directionans = 0;
angle = 360;
% Begin your code after this line
vertical=[0 1];

airspeed=groundspeed-windspeed;

if (airspeed(1)>0 && airspeed(2)>0)
   direction='NE';
elseif all(airspeed==airspeed.*north)
    direction='N';
elseif (airspeed(1)<0 && airspeed>0)
    direction='NW';
elseif all(airspeed==airspeed.*south)
    direction='S';
elseif (airspeed(1)>0 && airspeed(2)<0)
    direction='SE';
elseif (airspeed(1)<0 && airspeed(2)<0)
    direction='SW';
elseif all(airspeed==airspeed.*west)
    direction='W';
elseif all(airspeed==airspeed.*east)
    direction='E';
end

if airspeed(2)<0
    vertical=south;
elseif airspeed(2)>0
    vertical=north;
end
vertical;

angle=acosd(dot(airspeed,vertical)/(norm(airspeed)*norm(vertical)));
[norm(airspeed) direction angle]

我想要一个表格中的输出

代码语言:javascript
复制
[50 'NW' 80]

相反,结果是

代码语言:javascript
复制
'EZ'

这可能是由于错误地将字符赋值给变量造成的。我们能做些什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-06 02:48:24

不能将数字放入字符数组,也不能将字符放入数字数组。要混合数据类型,需要使用单元格数组,这是用大括号实现的:

代码语言:javascript
复制
>> {norm(airspeed) direction angle}
ans =
  1×3 cell array
    [50]    'NW'    [80]

我希望[50 'NW' 80][char(50) 'NW' char(80)]相同,后者等于2NWP

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47128918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档