简体   繁体   English

如何将矩阵中的列乘以不同的常数(每列),然后在Matlab中将每一行求和?

[英]How to multiply a columns in a matrix by a different constant (per column) and then summing each row up in Matlab?

For a project, I have a set amount of orders coming in and I am trying to calculate the total for each total. 对于一个项目,我有一定数量的订单进来,我正在尝试计算每个总数的总数。 After multiplying each column (number of each item) of the matrix by a set constant (each price pertains to a different column in the matrix), I would like to sum all the cells in each row so that I can find the total price per each order. 将矩阵的每一列(每件商品的数量)乘以设置的常数(每个价格与矩阵中的不同列有关)后,我想对每一行中的所有单元格求和,以便可以找到每行的总价每个订单。 This is the code I have so far: 这是我到目前为止的代码:

%A is the matrix of item types and quantities of each item

%A = |OrderNumber  Kitkat   Hershey   Reese's .....  Rolo|
     |  1            3        4        2      .....   4  |
     |  2            4        10       9      .....   2  |
     |  3            7        8        0      .....   0  |
     |.....         ....     ...      ...     ..... .....|

candyPrice = [3 4 3 ........];
orderTotalPrice = {}; 
for i = 1:10
  for k = 2:10
    orderTotalPrice(i) = A(i,k).*candyPrice(k)+orderTotalPrice;        
end
end

I am getting error messages when I run this code, saying that "Undefined function 'plus' for input arguments of type 'cell'." 运行此代码时,我收到错误消息,说“类型为“ cell”的输入参数的未定义函数“ plus”。”

For the question title: 对于问题标题:

" multiply columns in a matrix by a different constant (per column) and then summing each row up in Matlab? " 将矩阵中的列乘以不同的常数(每列),然后在Matlab中将每一行相加?

one symbol, * will do this, (as mentioned in comments as vector-matrix product) 一个符号*将执行此操作(如评论中提到的矢量矩阵乘积)

What you are trying to calculate is equivalent to 您要计算的内容等同于

orderTotalPrice = candyPrice*matA

where candyPrice is a 1xn vector and matA is nxm (n being the number of item types and m being the number of orders...) 其中candyPrice是1xn向量,而matA是nxm(n是商品类型的数量,m是订单数量...)

For OP's code 对于OP的代码

To use this method with the cell A we need to 要将这种方法用于单元格A,我们需要

  • Remove the order numbers and titles 删除订单号和标题
  • Convert from cell to matrix 从单元格转换为矩阵
  • Transpose (to be numitemtypes x numorders) 转置(为numitemtypes x numorders)

So the one-liner which performs all this is: 因此,执行所有这些操作的单一代码是:

orderTotalPrice = candyPrice*cell2mat(A(2:end,2:end)).'

with the test data 与测试数据

A = 

    'orderNo'    'item1'    'item2'    'item3'
    [      1]    [    2]    [    1]    [    3]
    [      2]    [    3]    [    2]    [    3]
    [      3]    [    1]    [    3]    [    3]


candyPrice =

   100    10     1

we get 我们得到

orderTotalPrice = candyPrice*cell2mat(A(2:end,2:end)).'

orderTotalPrice =

   213   323   133

Use p1 = bsxfun(@times,A(:,2:end),candyPrice) to compute the prices of all products. 使用p1 = bsxfun(@times,A(:,2:end),candyPrice)计算所有产品的价格。 The total order price for each candy is p2 = sum(p1,2) and the total price is p = sum(p2) . 每个糖果的总订购价格为p2 = sum(p1,2) ,总价格为p = sum(p2)


Alternatively, you adjust your code like this: 或者,您可以这样调整代码:

candyPrice = [3 4 3 ........];
orderTotalPrice = zeros(1,10);
for i = 1:10
    for k = 2:10
        orderTotalPrice(i) = A(i,k).*candyPrice(k)+orderTotalPrice(i);
    end
end
total = sum(orderTotalPrice);

First of all, the orderNumber should not be multiplied with anything, otherwise it won't make any sense. 首先,orderNumber不应与任何东西相乘,否则将毫无意义。

Therefore, I assume that size(candyPrice,2) equals size(A,2)-1 因此,我假设size(candyPrice,2)等于size(A,2)-1

Then the vectorized code would be as follows: 然后,矢量化代码如下:

orderTotalPrice = sum(repmat(candyPrice,[size(A,1) 1]).*A(:,2:end),2);

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

相关问题 Matlab将矩阵中的每一行乘以不同的数字 - Matlab multiply each row in matrix by different number 如何将矩阵A的每一列乘以矩阵B的每一行,并在Matlab中求和矩阵? - How to multiply each column of matrix A by each row of matrix B and sum resulting matrices in Matlab? MATLAB:将矩阵A中的每一列乘以矩阵B中的一行 - MATLAB: Multiply each column in Matrix A by a row in Matrix B 如何在matlab中将每一行与另一个矩阵元素的每一行相乘? - how to multiply each row with each row of another matrix elementwise in matlab? 将矩阵中的每一列与另一列中的对应行相乘,然后在Matlab中求和 - Multiply each column in a matrix by corresponding row in another and sum results in Matlab 在Matlab中总结两个矩阵的特殊列 - Summing up special columns of two matrix in Matlab 对双精度矩阵中每一行的特定列求和 - Summing specific columns for each row in a matrix of double Matlab:按每列不同元素的大小对矩阵列进行排序 - Matlab: Sort matrix columns by size of different elements per column MATLAB:将矩阵中的值相加,直到一列的阈值水平 - MATLAB: summing values in matrix up to a threshold level of one column 如何在MATLAB中为矩阵中的每一行排列列? - How to permutate over columns for each row in a matrix in MATLAB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM