Adding the first row of a matrix to every row
3 views (last 30 days)
Show older comments
Hello,
Question asks to Create a matrix A of 'm'x'n' defined by: A(i,j)=ij+i if i is odd, and A(i,j)=i+jif i is even.
I've got that in the bag but there is a operation step that I'm not having any luck with. "Add the first row to every row other than the first row" My presumption was using the operation A(i,:) = A(i,:) - A(1,:) however the problem that I'm coming across is that testing any values of m and n above 2 would lead to only the last row being operated on. is that line of code inappropriate for the operation?
1 Comment
Jan
on 18 Apr 2018
@amy wang: A confusing question.
Add the first row to every row
Sounds like a job for the operator "+", but you use "-".
testing any values of m and n above 2 would lead to only the
last row being operated on
It seems like there is a problem in your code, but you do not post it. Then guessing the reason is hard.
What is the relation between
A(i,j)=ij+i if i is odd, and A(i,j)=i+j if i is even
and
Add the first row to every other row
?
Answers (2)
KSSV
on 18 Apr 2018
Many methods are possible, one of the way:
A = rand(3,3) ; iwant = A+A(1,:) ; % Add first row to all the rows iwant(1,:) = A(1,:) ; % Replace the first row of result with first row of A
0 Comments
Jan
on 18 Apr 2018
A = rand(3, 4); A(2:3, :) = A(2:3, :) + A(1, :); % Auto-expand since R2016b
For older versions:
A(2:3, :) = bsxfun(@plus, A(2:3, :), A(1, :));
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices 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!