If you have a table called A. And this table has three columns. How can you replace column three with column two?

6 Comments

Paolo
Paolo on 24 May 2018
Edited: Paolo on 24 May 2018
A = [1 2 3; 2 3 4; 3 5 6]
A =
1 2 3
2 3 4
3 5 6
A(:,3)=A(:,2)
A =
1 2 2
2 3 3
3 5 5
Eliah, I have noticed you have asked a few questions, however have never selected an answer as accepted. This would be helpful for the community as it makes helpful solutions stand out, which could then help other people who are facing the similar issues.
The above will not work exactly as written for a table() object. However you can use
A{:,3} = A{:,2};
I get the message Index exceeds matrix dimensions. Do you knwo why?
Are you sure that you are using a table() object and not a cell array or numeric array? And are you sure that your table object has three columns already?
Finnaly there is a working code. Probably re you right and was I working with a cell array. I have changed a lot of things untill it worked. Trail and error.
Please do not close questions that have an Answer.

Sign in to comment.

 Accepted Answer

Akira Agata
Akira Agata on 25 May 2018
Edited: Akira Agata on 25 May 2018
If you have the latest version (R2018a), you can simply do it by movevars function, like:
yourTable = movevars(yourTable,2,'After',3);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!