Adding row 2 and row 3 and multiplying it by 3
Show older comments
A= [ 1 1 1 1 1 1 1 1 1 1 6 6 6 6 6 1 1 1 1 1 1 1 1 1 1 ]
10 Comments
'A' only has 1 row.
These lines below add rows 2 and 3 and then multiplies the result by 3 but you haven't specified whether rows 2 and 3 should be added by row or by column.
sum(x([2,3],:)) .* 3
% or
sum(x([2,3],:),2) * 3
Renesse Cammock
on 4 Oct 2019
Edited: Renesse Cammock
on 4 Oct 2019
Star Strider
on 4 Oct 2019
Please define row 1, row 2, and row 3.
Renesse Cammock
on 4 Oct 2019
Renesse Cammock
on 4 Oct 2019
@Renesse Cammock, in response to the PM, let's continue the discussion here. Which solution worked for you? The first one proposed (my comment above) or the second one proposed (Star Strider's in the answer section, half of which is virtually the same as mine)? And how would you like the change the output?
Renesse Cammock
on 4 Oct 2019
Adam Danz
on 4 Oct 2019
You'll have to provide an example of an input and an expected output.
Renesse Cammock
on 4 Oct 2019
Adam Danz
on 4 Oct 2019
That's a little better but it's still not clear.
Row 2 + row 3 is merely [1 1 1 1 1] + [1 1 1 1 1] which equals [2 2 2 2 2] if we're adding each element together.
[2 2 2 2 2] * 3 is just [6 6 6 6 6].
The question remains, how would that fit into a matrix? In your output example it's in row 3 but I'm not sure why.
Answers (4)
Star Strider
on 4 Oct 2019
The semicolons make all the difference.
I’m not at all certain what you want to do. Try this:
A = [1 1 1 1 1; 1 1 1 1 1 ; 6 6 6 6 6 ; 1 1 1 1 1; 1 1 1 1 1];
R23x3 = 3*(A(2,:)+A(3,:));
Anew = [A(1,:); R23x3; A(4:end,:)]; % Replace A([2 3],:) wisth ‘R23x3’
Experiment to get the result you want.
2 Comments
Renesse Cammock
on 4 Oct 2019
Star Strider
on 4 Oct 2019
My pleasure.
If my Answer helped you solve your problem, please Accept it!
Adam Danz
on 4 Oct 2019
A= [
1 1 1 1 1;
1 1 1 1 1;
1 1 1 1 1;
1 1 1 1 1;
1 1 1 1 1];
A(3,:) = sum(A([2,3],:)) * 3;
% ^ Not sure why this should go in row 3 but that matches your output example
Result
A =
1 1 1 1 1
1 1 1 1 1
6 6 6 6 6
1 1 1 1 1
1 1 1 1 1
4 Comments
Adam Danz
on 4 Oct 2019
"If u were multiplying first is there a specific function I should put at the front like how to add sum it at the front?"
A(3,:) = prod(A([2,3],:)) + 3;
% ^^^^
But 1 * 1 = 1 so I doubt that's what you want to do.
"How to write a single matlab code to multiply rows 1 and 2 by 2 then square the result" [from PM]
prod(A([1,2],:)).^2
But again, if you're dealing with all 1s, then the result will still be all 1s because 1*1 =1 and 1^2=1.
Renesse Cammock
on 4 Oct 2019
Sorry, I missread. My previous comment multiplied row 1 by row 2. Instead, we're supposed to multiply row 1 & 2 by 2 (and then square it).
(A([1,2],:) *2).^2
while will give you [2 x 5] matrix of 4s since 1*2 = 2 and 2^2 = 4.
Adam Danz
on 4 Oct 2019
BTW, I think this will all be clear to you if you spend a few minutes reading about indexing in Matlab.
Renesse Cammock
on 4 Oct 2019
0 votes
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!