Deleting Row and Column

조회 수: 8 (최근 30일)
Imani DaSilva
Imani DaSilva 2020년 4월 24일
편집: Imani DaSilva 2020년 4월 25일
I am doing a project with matrices. I want to change the size of the matrix by deleting the last 2 rows and the last 2 column. I want to go from 0.01 to 0.15 in steps of 0.01. When I display Q1 I am able to delete the last 2 columns but the the last 2 rows and then when I try to delete the rows (disp Q2) it deleted the rows but not the columns. I want BOTH the last two ROW AND last two COLUMN to be deleted from the Matrix P. Any help is appreciated!!!!
P = [0.1 0.75 0 0 0.15 0; 0 0.1 0.8 0 0.10 0; 0 0 0.15 0.75 0.1 0; 0 0 0 0.1 0.1 0.8; 0 0 0 0 1 0; 0 0 0 0 0 1];
disp(P)
for v = 0.01:0.01:0.15
P(1,1) = v + P(1,1);
%disp(P)
Q1 = ([:,1 2 3 4]);
disp(Q1)
Q2 = P([1 2 3 4],:);
%Q2 = P(:, [5 6]);
disp(P)
end

답변 (2개)

KSSV
KSSV 2020년 4월 24일
If A is your matrix, you can remove last two rows and columns using:
[m,n] = size(A) ;
A(m-1:m,:) = [] ; % removes last two rows
A(:,n-1:n) = [] ; % removes last two columns
  댓글 수: 2
Imani DaSilva
Imani DaSilva 2020년 4월 24일
when I try this method i am getting an error saying "Index in position 1 is invalid. Array indices must be positive integers or logical values." it prints out the first matrix correctly and then runs into that error.
KSSV
KSSV 2020년 4월 24일
Show us the code which you have tried.

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


James Tursa
James Tursa 2020년 4월 24일
Q = P(1:end-2,1:end-2);
  댓글 수: 3
Imani DaSilva
Imani DaSilva 2020년 4월 24일
from what i understand P(1:end-2,1:end-2) is for the subtracting the last two rows and colums starting from column 1 and row 1. So if i wanted to get the last 2 columns without the last 2 rows. I tried the same concept with removing the last 2 rows but extracting the last 2 columns and it is just giving me a vector of 1's. But i would want
0.1500 0
0.1000 0
0.1000 0
0.1000 0.8000
(1.0000 0) ->minus this row
(0 1.0000) -> minus this row
Imani DaSilva
Imani DaSilva 2020년 4월 24일
I figured it out! thanks for the help

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

카테고리

Help CenterFile Exchange에서 Circuit Envelope Simulation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by