【问题标题】:Divide a matrix by a vector将矩阵除以向量
【发布时间】:2020-05-26 12:05:42
【问题描述】:

问题:我正在尝试将 3x63 矩阵(它的每一行)除以 1x63 向量(这是矩阵的最后一行)并且 matlab 对我大喊那 Matrix dimensions must agree. 我在这里做错了什么?

coords_ = load('m1.mat')
x2D = coords_ .x2D
[rows, columns] = size(x2D)
last_row_2d = x2D(end ,:)
mat_2d = repmat (x2D ,[rows columns])
new_mat = mat_2d ./ last_row_2d

【问题讨论】:

  • 您作为答案发布的解决方案更好(即使用广播),但只是指出您的初始方法mat_2d = repmat (x2D ,[rows columns]) 的错误在这里您不想重复列,只有行。否则mat_2d 的尺寸将是 3 行 x 63*63 列
  • @Dan 是的,我使用了调试器并查看了它的尺寸,并明白这个功能对我没有帮助。谢谢丹。
  • 它本来可以工作的,只是效率不高。您需要这样做:repmat (x2D ,[rows 1]) 只重复行。但是广播比使用repmat 更有效。
  • 哦,我明白了。是的,我总是在带有数组的 python 中使用广播,所以这是有道理的。谢谢!

标签: matlab


【解决方案1】:

刚刚做到了,它奏效了:

last_row_2d = x2D(end ,:)
new_mat = x2D(1:2,:) ./ last_row_2d

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    相关资源
    最近更新 更多