简体   繁体   English

Matlab将矩阵中的每一行乘以不同的数字

[英]Matlab multiply each row in matrix by different number

Say that I have a matrix: 假设我有一个矩阵:

A = [ 1 2 3 ; 4 5 6 ; 7 8 9 ; 10 11 12];

Is there a way to multiply : 有没有办法繁殖:
row 1 by 1 第1行第1行
row 2 by 2 第2排2
row 3 by 3 第3行第3行
and so on? 等等?

I am able to do this with for loops, however it if for an assignment where they want us to use matrices. 我能够使用for循环执行此操作,但是如果对于他们希望我们使用矩阵的分配,它。 In the actual assignment A is filled with random number but each row which by multiplied consecutively. 在实际分配中, A用随机数填充,但每行连续相乘。

Thanks, any help is much appreciated 谢谢,非常感谢任何帮助

You just need to multiply a diagonal matrix by A like so. 你只需将对角矩阵乘以A就好了。

A = [ 1 2 3 ; 4 5 6 ; 7 8 9 ; 10 11 12];
disp(diag([1 2 3 4]) * A);

 1     2     3
 8    10    12
21    24    27
40    44    48

You can use bsxfun to accomplish this easily and very quickly 您可以使用bsxfun轻松快速地完成此任务

out = bsxfun(@times, [1 2 3 4].', A)

In newer versions of MATLAB (R2016b and newer) you can actually replace bsxfun with simply * 在较新版本的MATLAB(R2016b和更新版本)中,您实际上可以简单地用*替换bsxfun

out = [1 2 3 4].' * A;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在matlab中将每一行与另一个矩阵元素的每一行相乘? - how to multiply each row with each row of another matrix elementwise in matlab? Matlab中不同位置数的矩阵的每一行的左圆移位 - Left circular shift in Matlab for each row of a matrix of a different number of positions MATLAB:将矩阵A中的每一列乘以矩阵B中的一行 - MATLAB: Multiply each column in Matrix A by a row in Matrix B 如何将矩阵中的列乘以不同的常数(每列),然后在Matlab中将每一行求和? - How to multiply a columns in a matrix by a different constant (per column) and then summing each row up in Matlab? 将矩阵中的每一列与另一列中的对应行相乘,然后在Matlab中求和 - Multiply each column in a matrix by corresponding row in another and sum results in Matlab 如何将矩阵A的每一列乘以矩阵B的每一行,并在Matlab中求和矩阵? - How to multiply each column of matrix A by each row of matrix B and sum resulting matrices in Matlab? 矩阵的每一行乘以另一个矩阵 - Multiply each row of a matrix by another matrix 在MATLAB中将每个子块与矩阵相乘 - Multiply each sub-block with a matrix in MATLAB 使用不同的输入将repmat应用于Matlab中矩阵的每一行 - Applying repmat to each row of a matrix in Matlab with different inputs 将矩阵的每一行与其转置的自身相乘 - Multiply each row of a matrix with its transposed self
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM