how can I edit a matrix and rename it at the same time?

조회 수: 5 (최근 30일)
Matthew Perry
Matthew Perry 2019년 11월 25일
댓글: Matthew Perry 2019년 11월 25일
I start with a random matrix and edit the 8th column to new numbers and then rename it. what I thought was this:
my_matrix = rand(4,9)
my_matrix(:,8) = [2;4;6;1]
edited_matrix = my_matrix
however, I want to rename and edit the matrix in one go so at the end of the script, I have two matrices...
Almost like this (but this doesnt work)
my_matrix = rand(4,9)
edited_matrix = [my_matrix(:,8) = [2;4;6;1]]
so at the end I have two variables, my_matrix and edited_matrix that are different and can be reffered to separately

채택된 답변

Guillaume
Guillaume 2019년 11월 25일
편집: Guillaume 2019년 11월 25일
Sorry, you're better off doing it in two lines. You want to copy the matrix into the new variable before changing it so:
edited_matrix = my_matrix;
edited_matrix(:, 8) = [2 4 6 1];
If you really want to do it in just one line, you could do:
edited_matrix = [my_matrix(:, 1:7), [2;4;6;1], my_matrix(:, 9:end)];
but it's actually less readable and probably slower to execute.

추가 답변 (1개)

Thorsten
Thorsten 2019년 11월 25일
You can do it like this:
my_matrix = rand(4,9);
edited_matrix = my_matrix;
edited_matrix(:,8) = [2;4;6;1];

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by