repmat功能无法正常工作 [英] repmat function does not work properly

查看:115
本文介绍了repmat功能无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑以下情况,例如,我们给定了矩阵,并且想使此矩阵在列中居中,所以为零.

let us consider following situation,for instance we have given matrix and we want to make zero centered this matrix in columns,so

A=rand(4,3)

A =

    0.6948    0.4387    0.1869
    0.3171    0.3816    0.4898
    0.9502    0.7655    0.4456
    0.0344    0.7952    0.6463

现在这两种方法都可以正常工作

now this two method works properly

A-repmat(mean(A),size(A,1),1)

ans =

    0.1957   -0.1565   -0.2553
   -0.1820   -0.2137    0.0476
    0.4511    0.1703    0.0035
   -0.4647    0.1999    0.2042

还有

bsxfun(@minus,A,mean(A))

ans =

    0.1957   -0.1565   -0.2553
   -0.1820   -0.2137    0.0476
    0.4511    0.1703    0.0035
   -0.4647    0.1999    0.2042

但是以某种方式无法使用

but somehow following method does not work

B=mean(A)

B =

    0.4991    0.5953    0.4421

 A-repmat(B,1,4)

ans =

     0     0     0     0     0     0     0     0     0     0     0     0

我已经尝试过移调,但是

i have tried transpose but

A-repmat(B,1,4)'
Error using  - 
Matrix dimensions must agree.

我也尝试过跟踪

A-repmat(B,1,3)'
Error using  - 
Matrix dimensions must agree.

>> A-repmat(B,1,3)
Error using  - 
Matrix dimensions must agree.
 so what is problem of  failure of this method?

推荐答案

您没有对repmat函数使用正确的语法

You are not using proper syntax for the repmat function

在您的示例中,您需要使用repmat

In your example ,You need to create a matrix of size 4 x 3 using repmat

现在,对repmat(A,k,j)的调用沿第一个维度(即垂直)重复A k次,并沿第二个维度(即水平)重复j次.

Now a call to repmat(A,k,j) repeats matrix A k times along the first dimension (i.e vertically) and j times along the second dimension (i.e, horizontally).

在这里,您需要在第一维上重复矩阵mean 4次,在第二维上重复1次.

Here, you need to repeat matrix mean 4 times in first dimension and 1 time in second dimension .

因此,对repmat的正确调用是repmat(mean,4,1)

Hence, the correct call to repmat is repmat(mean,4,1)

repmat(B,4,1)

ans =

    0.4991    0.5953    0.4421
    0.4991    0.5953    0.4421
    0.4991    0.5953    0.4421
    0.4991    0.5953    0.4421

您似乎需要知道方法失败的原因

It looks like you need to know why your method is failing

repmat(B,1,4) %// returns a 1x12 matrix while A is 3x4 matrix hence dimensions do not agree while using @minus
ans =

    0.4991    0.5953    0.4421   0.4991    0.5953    0.4421   0.4991    0.5953    0.4421   0.4991    0.5953    0.4421

这篇关于repmat功能无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆