sum all successive values in matrix row

2 ビュー (過去 30 日間)
summyia qamar
summyia qamar 2018 年 8 月 13 日
コメント済み: summyia qamar 2018 年 8 月 13 日
I have a vector A which represents 10 machines defect rate. Matrix B shows a process plan in each row in which the elements show process time of one part on corresponding machine for example; row 1 col 4 shows that this part takes 9 minutes to process on machine 4.I want to multiply each row of B with A such that every non zero element of B is multiplies by the sum of corresponding non zero elements of A. for example;
A=[0.1, 0.2, 0.2, 0.4, 0.2, 0.3, 0.1, 0.1, 0.1, 0.2]
B=[0 0 0 9 0 0 10 0 8 8;
8 0 0 0 7 9 0 0 0 10]
C=[0 0 0 (9+9*(0.4+0.1+0.1+0.2)) 0 0 (10+10*(0.1+0.1+0.2) 0 (8+8*(0.1+0.2)) 8+8*0.2;
(8+8*(0.1+0.2+0.3+0.2) 0 0 0 (7+7*(0.2+0.3+0.2) (9+9*(0.3+0.2)) 0 0 0 (10+10*(0.2))]
I tried cumsum and move sum but they are not giving the right answer.
  3 件のコメント
summyia qamar
summyia qamar 2018 年 8 月 13 日
A is 1x10 and B is 2x10 so each row of B is compared with A. starting from row 1, 4th element of B is non zero so it is multiplied with A 4th element of A in such a way that all the successive elements of A corresponding to non zero elements of B are added and then multiplied with B.
summyia qamar
summyia qamar 2018 年 8 月 13 日
non zero elements of B are 4th, 7th 9th and 10th so these elements of A are added and multiplied with 4th element of B. then for 7th element; 7 9 and 10th elements of A are added and them multiplied with 7th element of B

サインインしてコメントする。

採用された回答

Matt J
Matt J 2018 年 8 月 13 日
編集済み: Matt J 2018 年 8 月 13 日
map=(B~=0);
C=cumsum(map.*A,2,'reverse').*B+B
  1 件のコメント
summyia qamar
summyia qamar 2018 年 8 月 13 日
yes this worked. thankyou sir

サインインしてコメントする。

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2018 年 8 月 13 日
summiya - for the seventh element, you seem to have
(10+10*(0.3+0.1+0.2)
Is the 0.3 a mistake and so should be 0.1 instead?
Regardless, if we assume that B has integers only, then we can determine the indices of the non-zero elements of the first row of B with
nonZeroElementsInB = B(1,:) ~= 0;
We can then determine the sum (from A) of these non-zero elements as
sumNonZeroElementsInA = sum(A(nonZeroElementsInB));
So this will be the sum of all the elements of A corresponding to non-zero elements in the first row of B
(0.4+0.1+0.1+0.2)
You will need to adjust this sum for those elements where you don't want to include all of these values.
  1 件のコメント
summyia qamar
summyia qamar 2018 年 8 月 13 日
yes thank you for correction.I corrected 0.3 error in question

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by