Creating a Matrix that is a manipulation of another matrix

조회 수: 4 (최근 30일)
Julian
Julian 2015년 10월 6일
답변: Julian 2015년 10월 6일
Hi, I'm very new to Matlab, and I have a question about creating a matrix. If there is a matrix A = [a b ; c d], how do you create a matrix = [a -b ; c -d]? So the second column entries of A are multiplied by -1.
Thank you.

채택된 답변

dpb
dpb 2015년 10월 6일
A(:,2)=-A(:,2); % negate elements in original
or
A(:,2)=-1*A(:,2);
or
B=[A(:,1) -A(:,2}]; % new array keeping old
Any number of additional ways are possible including, of course linear algebra methods...
B=A*[1 0;0 -1];

추가 답변 (4개)

Joseph Cheng
Joseph Cheng 2015년 10월 6일
편집: Joseph Cheng 2015년 10월 6일
There are lots of ways how about element by element multiplication like:
x = magic(4)
trans = [ones(4,2) -ones(4,2)]
x.*trans
which if it gets complicated say corners and such you can build the trans matrix to whatever you want.
or by selecting the columns manually:
x(:,3:4) = -x(:,3:4)

Julian
Julian 2015년 10월 6일
Thanks!

Julian
Julian 2015년 10월 6일
I have another question. How do you replace just one element in a column not the entire elements in the column?
  댓글 수: 1
Joseph Cheng
Joseph Cheng 2015년 10월 6일
so the indexing is something you should spend some time to read about to fully understand but its like:
x(row,column) so you can say x(2,3) which will specifically be the element of intersection of row 2 and column 3. so to replace one element you would just specify that one element. the : marker in the examples everyone is giving is the "all" marker for all rows/columns (depending on where it is)
x(:,2) is all rows in column 2
x(2,:) is for all columns for row 2.

댓글을 달려면 로그인하십시오.


Julian
Julian 2015년 10월 6일
Thank you all!

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by