Reverse the value in multiple columns

1 次查看(过去 30 天)
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!

回答(1 个)

James Tursa
James Tursa 2017-5-16
编辑:James Tursa 2017-5-16
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

类别

Help CenterFile Exchange 中查找有关 NaNs 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by