具有定义数组的矩阵除法 [英] Matrix division with defined Array

查看:75
本文介绍了具有定义数组的矩阵除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将矩阵"A"的每个元素除以"B"的每个元素. 我想制作一个新的4"C"矩阵

A=[5    6   9    1; 3    8     9   5; 5    4    2    0;7    8    2    1]
B=[-0.1125,-0.0847,-0.0569,-0.0292]
C =   A ./ B

但我收到

的错误

在赋值A(I)= B中,B和I中的元素数必须为 一样.

我该如何解决该问题?

解决方案

尝试一下

C = A./repmat(B,size(A,1),1);

使用@SHAI的答案,因为它更快.这是一些统计数据

n = 100;
k = 100;
A = randi(1000,n,n);
B = randi(1000,1,n);
#mine method
tic;
for i = 1:k
    C = A./repmat(B,size(A,1),1);
end
mine = toc

mine =

    1.2330 seconds

#Shai's method
tic;
for i =1:k
    C = bsxfun(@rdivide, A, B );
end
shai = toc

shai =

    0.1085 seconds

如果您可以给我一个大致的尺寸,我可以给您一个更笼统的答案

i am trying to divide each element of Matrix "A" with each element of "B". I want to make a new matrix of 4 "C"

A=[5    6   9    1; 3    8     9   5; 5    4    2    0;7    8    2    1]
B=[-0.1125,-0.0847,-0.0569,-0.0292]
C =   A ./ B

but i am getting error of

In an assignment A(I) = B, the number of elements in B and I must be the same.

how i fix that issue?

解决方案

try this

C = A./repmat(B,size(A,1),1);

Use @SHAI's answer as it is faster. Here are some stats

n = 100;
k = 100;
A = randi(1000,n,n);
B = randi(1000,1,n);
#mine method
tic;
for i = 1:k
    C = A./repmat(B,size(A,1),1);
end
mine = toc

mine =

    1.2330 seconds

#Shai's method
tic;
for i =1:k
    C = bsxfun(@rdivide, A, B );
end
shai = toc

shai =

    0.1085 seconds

I can give you a more general answer if you would give me general dimensions

这篇关于具有定义数组的矩阵除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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