## How to sum a row of variables during a loop

Stephen

### Stephen (view profile)

さんによって質問されました 2019 年 3 月 16 日

### Star Strider (view profile)

さんによって コメントされました 2019 年 3 月 16 日
Star Strider

### Star Strider (view profile)

さんの 回答が採用されました
Hi, dont know if this is possible but I'll ask anyway.
I have a 100*2 matrix. I want to check if each value equals 1 or zero, if it's 1 multiply it by some number and have that saved as some new variable. I then want to sum those variables per row and store those totals in a new 100*1 matrix.
I understand this code doesnt make actual sense but it indicates what im trying to achieve. Can anyone point me in the right direction?
Thanks in advance to anyone for help, it's really appreciated.
val1 = 0;
val2 = 0;
multiplier1 = 10
multiplier2 = 15
for i = 1:100
if matrix(i,1) == 1
val1(i) = matrix(i,1)*multiplier1
else
end
if matrix(i,2) == 1
val2(i) = matrix(i,2)*multiplier2
else
end
rowtotal(i) = val1(i) +val2(i)
end

### madhan ravi (view profile)

2019 年 3 月 16 日
It looks trivial, what do you want to do when no of val1 is not equal to val2?
Stephen

### Stephen (view profile)

2019 年 3 月 16 日
Hi madhan, thanks for commenting. This example is probably too far from what Im actually trying to achieve, I asked a more complete question in a comment below if you are interested in lending a hand.
Thanks.

サインイン to comment.

R2018b

## 1 件の回答 ### Star Strider (view profile)

2019 年 3 月 16 日
採用された回答

See if this does what you want:
matrix = randi([0 1], 100, 2); % Create ‘matrix’
multiplier1 = 10;
multiplier2 = 15;
val = bsxfun(@times, matrix, [multiplier1, multiplier2]);
rowtotal = sum(val,2);
Note that you do not have to test for a given element being 0 or 1, since the 0 values will remain 0 and only the 1 values will be multiplied.

Star Strider

### Star Strider (view profile)

2019 年 3 月 16 日
This is a bit more efficient, and runs without error. You will have to determine if it does what you want:
LPCopen = 0;
HPCopen = 0;
HPTopen = 0;
LPTopen = 0;
cyclesnew = randi(20000, 100, 1); %100*1 array of cycles between 0 and 20000
replace = randi([0 1], 100,4); % 100* 4 matrix of random 0-1 to indicate replacement
population = [(LPCopen+cyclesnew), (HPCopen+cyclesnew), (HPTopen+cyclesnew),(LPTopen+cyclesnew) ] ; %100*4 matrix of CSN
%indexing to alternate columns in replace + pop
finalpop = replace(:,[1;1]*(1:size(replace,2)));
finalpop(:,1:2:end) = population;
cycleprof = 10;
LM = finalpop(:,2:2:end) == 0;
LPCsale = zeros(size(finalpop,1),1);
HPCsale = zeros(size(finalpop,1),1);
LPTsale = zeros(size(finalpop,1),1);
HPTsale = zeros(size(finalpop,1),1);
LPCsale(LM(:,1)) = finalpop(LM(:,1),1)*cycleprof;
HPCsale(LM(:,2)) = finalpop(LM(:,2),1)*cycleprof;
LPTsale(LM(:,3)) = finalpop(LM(:,3),1)*cycleprof;
HPTsale(LM(:,4)) = finalpop(LM(:,4),1)*cycleprof;
totalsale = LPCsale + HPCsale + HPTsale + LPTsale;
This uses ‘logical indexing’ to eliminate the loops and the if block tests. Otherwise, it is essentially your original code.
Stephen

### Stephen (view profile)

2019 年 3 月 16 日
Wow thank you so much this is perfect!
I must read up on logical indexing.
Thanks again I really appreciate you taking the time to put this together, you're a life saver!!
Star Strider

### Star Strider (view profile)

2019 年 3 月 16 日
As always, my pleasure!

サインイン to comment.