ithinksoim 发表于 2011-7-21 19:27:52

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

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.

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

我的运行环境:
>> computer
ans =
PCWIN
>> version
ans =
7.12.0.635 (R2011a)

ithinksoim 发表于 2011-7-21 19:29:59

我想知道哪儿有times规范,对上述情况给出说明?

qibbxxt 发表于 2011-7-22 10:40:21

问题不在times,而不是数据类型不同
一个是double类型的数组,一个是int32型的标量,当然不能相乘
你可以试一试>> int32(vs).*int32(1)或者>> vs.*1都是没有问题的

ithinksoim 发表于 2011-7-22 10:50:39

按你的解释,那么为什么int32(1:5).*double(1)又是可以的呢?

guocong89 发表于 2011-7-22 11:09:52

错误说的很明白,int32只能够和int32或者标量double求乘积,也就是说1*int32(1)

是可以的,
但是不能够和double向量进行计算
因此 .*int32(1)是错误的

ithinksoim 发表于 2011-7-22 12:23:05

错误说的很明白,int32只能够和int32或者标量double求乘积,也就是说1*int32(1)

是可以的,
但是不能够和double向量进行计算
因此 .*int32(1)是错误的
guocong89 发表于 2011-7-22 11:09 http://forum.simwe.com/images/common/back.gif

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

messenger 发表于 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?
页: [1]
查看完整版本: 关于.*运算(times)的一个问题