Divide every entry of a column by the same number

14 views (last 30 days)
Suppose I have
A = [1 2 3; 4 5 6; 7 8 9];
and I want to divide every entry in the second column by 2 so the answer is
B = [1 1 3; 4 2.5 6; 7 4 9];
What will be a compact way of doing this?

Accepted Answer

Birdman
Birdman on 22 Mar 2018
Edited: Birdman on 22 Mar 2018
A(:,2)=A(:,2)./2;
B=A
  2 Comments
Walter Roberson
Walter Roberson on 29 Oct 2019
Anish Thakur:
It does work. But the code above has the side effect of changing A as well. A small modification avoids that:
B=A;
B(:,2)=B(:,2)./2;

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!