Reverse the value in multiple columns

1 view (last 30 days)
Jinah Lee
Jinah Lee on 16 May 2017
Edited: James Tursa on 16 May 2017
Hello, I have 564*31 matrix, and I have inverse the values in column 6,24,35,and 46. Values in these columns are 1,to 5 and I have reverse them. For example, I need to reverse 1 to 5, 2 to 4, 3 becomes 3, 4 to 2 and 5 to 1 in these column. How can I approach?
Thank you so much!

Answers (1)

James Tursa
James Tursa on 16 May 2017
Edited: James Tursa on 16 May 2017
x = your matrix
c = a vector with column numbers to reverse
x(:,c) = 6 - x(:,c);
e.g.,
>> x = randi(5,6,6) % <-- some sample data
x =
1 2 2 1 5 3
4 4 5 3 5 5
2 4 1 1 1 1
3 4 5 5 2 2
1 3 3 1 2 1
4 1 5 4 5 1
>> c = [1 3 6] % <-- columns to work on
c =
1 3 6
>> x(:,c) = 6 - x(:,c)
x =
5 2 4 1 5 3
2 4 1 3 5 1
4 4 5 1 1 5
3 4 1 5 2 4
5 3 3 1 2 5
2 1 1 4 5 5

Community Treasure Hunt

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

Start Hunting!