[英]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”。”
" 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是订单数量...)
To use this method with the cell A we need to 要将这种方法用于单元格A,我们需要
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.