找回密码
 注册
Simdroid-非首页
查看: 86|回复: 6

关于.*运算(times)的一个问题

[复制链接]
发表于 2011-7-21 19:27:52 | 显示全部楼层 |阅读模式 来自 湖南
times的docs是这样写的:
>> help times
.*  Array multiply.
    X.*Y denotes element-by-element multiplication.  X and Y
    must have the same dimensions unless one is a scalar.
    A scalar can be multiplied into anything.

    C = TIMES(A,B) is called for the syntax 'A .* B' when A or B is an
    object.


但是我执行下面的代码出错了:
  1. >> vs = 1:5
  2. vs =
  3.      1     2     3     4     5
  4. >> n = int32(1)
  5. n =
  6.            1
  7. >> vs.*n
  8. ??? Error using ==> times
  9. Integers can only be combined with integers of the same class, or scalar doubles.
复制代码


我的运行环境:
>> computer
ans =
PCWIN
>> version
ans =
7.12.0.635 (R2011a)
 楼主| 发表于 2011-7-21 19:29:59 | 显示全部楼层 来自 湖南
Simdroid开发平台
我想知道哪儿有times规范,对上述情况给出说明?
回复 不支持

使用道具 举报

发表于 2011-7-22 10:40:21 | 显示全部楼层 来自 河北廊坊
问题不在times,而不是数据类型不同
一个是double类型的数组,一个是int32型的标量,当然不能相乘
你可以试一试
  1. >> int32(vs).*int32(1)
复制代码
或者
  1. >> vs.*1
复制代码
都是没有问题的

评分

1

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2011-7-22 10:50:39 | 显示全部楼层 来自 广东河源
按你的解释,那么为什么int32(1:5).*double(1)又是可以的呢?
回复 不支持

使用道具 举报

发表于 2011-7-22 11:09:52 | 显示全部楼层 来自 北京
错误说的很明白,int32只能够和int32或者标量double求乘积,也就是说
  1. 1*int32(1)
复制代码


是可以的,
但是不能够和double向量进行计算
因此
  1. [1,2].*int32(1)
复制代码
是错误的

评分

1

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2011-7-22 12:23:05 | 显示全部楼层 来自 湖南邵阳
错误说的很明白,int32只能够和int32或者标量double求乘积,也就是说1*int32(1)

是可以的,
但是不能够和double向量进行计算
因此 [1,2].*int32(1)是错误的
guocong89 发表于 2011-7-22 11:09


对的,这个我知道。
我想问的是matlab中有那条语法规定了这点吗?而且times的docs中也没指出这点(事实上docs中指出 A scalar can be multiplied into anything.)。
作为一个编程语言,如果有未定义或没有明确规定的行为,让人很纠结。
回复 不支持

使用道具 举报

发表于 2011-7-22 13:25:48 | 显示全部楼层 来自 广东广州
Matlab以规范和例题详细著称,不可能没有这种规定,只是你没找到而已。
参考,Combining Integer Data Types with Type Double
However, you cannot combine an array of an integer data type with either of the following:
A scalar or array of a different integer data type
A scalar or array of type single

How do I multiply two 'int32' data type matrices in MATLAB?
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-10-6 10:29 , Processed in 0.039457 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表