Reverse the value in multiple columns

조회 수: 3 (최근 30일)
Jinah Lee
Jinah Lee 2017년 5월 16일
편집: James Tursa 2017년 5월 16일
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에서 Dynamic System Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by