Adding row 2 and row 3 and multiplying it by 3

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 Kommentare

Adam Danz
Adam Danz am 4 Okt. 2019
Bearbeitet: Adam Danz am 4 Okt. 2019
'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
Renesse Cammock am 4 Okt. 2019
Bearbeitet: Renesse Cammock am 4 Okt. 2019
row 2 and 3 should be added together by rows not columns
Please define row 1, row 2, and row 3.
How do I get the answer that I got back into the matrix?
[ 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]
Adam Danz
Adam Danz am 4 Okt. 2019
Bearbeitet: Adam Danz am 4 Okt. 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?
Both solutions works but none of them actually put it back in the matrix cause the question wanna a single matlab code which I never stated my apologies but how do I work it so that the answer is in the matrix form as in [ 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]
Adam Danz
Adam Danz am 4 Okt. 2019
You'll have to provide an example of an input and an expected output.
Write a single matlab code to add row 2 and 3 and multiply the result by 3 Input A= [1 1 1 1 1; 1 1 1 1 1;1 1 1 1 1 l;1 1 1 1 1;1 1 1 1 1] Output 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]
Adam Danz
Adam Danz am 4 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Star Strider
Star Strider am 4 Okt. 2019

0 Stimmen

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 Kommentare

Thanks
My pleasure.
If my Answer helped you solve your problem, please Accept it!

Melden Sie sich an, um zu kommentieren.

Adam Danz
Adam Danz am 4 Okt. 2019

0 Stimmen

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 Kommentare

"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.
It wouldn’t be 1 if u multiplying by 2 then squared
Adam Danz
Adam Danz am 4 Okt. 2019
Bearbeitet: Adam Danz am 4 Okt. 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
Adam Danz am 4 Okt. 2019
BTW, I think this will all be clear to you if you spend a few minutes reading about indexing in Matlab.

Melden Sie sich an, um zu kommentieren.

Renesse Cammock
Renesse Cammock am 4 Okt. 2019

0 Stimmen

That’s how I saw it on the question paper but it works thanks ??
Renesse Cammock
Renesse Cammock am 4 Okt. 2019

0 Stimmen

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?

1 Kommentar

Adam Danz
Adam Danz am 4 Okt. 2019
Bearbeitet: Adam Danz am 4 Okt. 2019
Please use the comment sections for discussion and the answer sections for answers. Otherwise the thread will become difficult to follow. The order of appearence of the answers is not fixed.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 4 Okt. 2019

Kommentiert:

am 4 Okt. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by