Deleting Rows of a Matrix

조회 수: 1 (최근 30일)
Chris Dan
Chris Dan 2020년 6월 25일
댓글: Chris Dan 2020년 6월 26일
Hello,
I have a 1D matrix or an array, I have attached files.
I have to delete its rows, such that starting from 4 till 6, then 10 to 12, then 16 till 18 and so on...
so basically leave three and then delete three.. leave 1 2 3 delete 4 5 6 then leave 7 8 9 delete 10 11 12... and going on till the last of matrix
I made this small program.
The problem I am having is in defining the variable X which tells how long will this loop run for until all the required rows are deleted.
load('Mass_Global.mat')
load('Damp_Global.mat')
load('Stiffness_Global.mat')
[V,D] = eigs(Stiffness_Global,Mass_Global,1,'smallestabs')
X =
a =4;
for i =1:1:X
for j =a:1:(a+2)
V(j,: )=[];
end
a = a+4
end
Does anyone has an idea?
Can it be done without loops?

채택된 답변

Image Analyst
Image Analyst 2020년 6월 25일
Here's one way:
v = rand(1, 1512)' % Create sample data.
m = reshape(v, 3, []) % Make into matrix.
m(:, 2:2:end) = []; % Delete every other column.
v2 = m(:); % Turn into column vector.
  댓글 수: 1
Chris Dan
Chris Dan 2020년 6월 26일
Thanks a lot for the answer!
How would it work if I have more than one column in my V matrix, like 2 or 3 or more.
Rows from 4 till 6 will be removed as it is, but how to bring (1,2) (2,2) and (3,2) below the original rows in m matrix?
like m matrix, instead of having 3 rows should have 6 rows, but the other three should come from the second column of V.

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

추가 답변 (1개)

Gaurav Aggarwal
Gaurav Aggarwal 2020년 6월 25일
편집: Gaurav Aggarwal 2020년 6월 25일
Hi Hamzah,
It is possible. Could you check if this works for you?
X = %% your value
%% Starting value of the last chunk to be deleted, simple Arithmetic Progression
an = (4+(X-1)*6);
%% Find all the starting values for the 'to be deleted' chunks i.e. [4 10 16 ... an]
L = 4:6:an;
%% [L L+1 L+2] are all the indices which need to be removed
V([L L+1 L+2], :) = [];
Let me know if this helps you. Thanks.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by