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 comentarios

Adam Danz
Adam Danz el 4 de Oct. de 2019
Editada: Adam Danz el 4 de Oct. de 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 el 4 de Oct. de 2019
Editada: Renesse Cammock el 4 de Oct. de 2019
row 2 and 3 should be added together by rows not columns
Star Strider
Star Strider el 4 de Oct. de 2019
Please define row 1, row 2, and row 3.
Renesse Cammock
Renesse Cammock el 4 de Oct. de 2019
How do I get the answer that I got back into the matrix?
Renesse Cammock
Renesse Cammock el 4 de Oct. de 2019
[ 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 el 4 de Oct. de 2019
Editada: Adam Danz el 4 de Oct. de 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
Renesse Cammock el 4 de Oct. de 2019
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 el 4 de Oct. de 2019
You'll have to provide an example of an input and an expected output.
Renesse Cammock
Renesse Cammock el 4 de Oct. de 2019
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 el 4 de Oct. de 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.

Iniciar sesión para comentar.

Respuestas (4)

Star Strider
Star Strider el 4 de Oct. de 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 comentarios

Renesse Cammock
Renesse Cammock el 4 de Oct. de 2019
Thanks
Star Strider
Star Strider el 4 de Oct. de 2019
My pleasure.
If my Answer helped you solve your problem, please Accept it!

Iniciar sesión para comentar.

Adam Danz
Adam Danz el 4 de Oct. de 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 comentarios

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

Iniciar sesión para comentar.

Renesse Cammock
Renesse Cammock el 4 de Oct. de 2019

0 votos

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

0 votos

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 comentario

Adam Danz
Adam Danz el 4 de Oct. de 2019
Editada: Adam Danz el 4 de Oct. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Oct. de 2019

Comentada:

el 4 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by