eliminate several columns and rows of a matrix using vectors.
2 views (last 30 days)
Show older comments
Pedro Guevara
on 9 May 2019
Commented: Pedro Guevara
on 9 May 2019
Good afternoon. Request your help with the following problem. I have a matrix called "Cons" - you can see it in the image -, and I have 2 vectors [Ii2, Ji2] that contain the position of rows and columns that I want to eliminate from the "Cons" matrix. For example, I want to eliminate the first 3 columns of "Cons" according to the value of the vector Ji2. How do I remove those columas and those same rows from my "Cons" matrix? Thank you.
0 Comments
Accepted Answer
per isakson
on 9 May 2019
Edited: per isakson
on 9 May 2019
Try
>> m = magic( 5 )
m =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
% eliminate column 1,2 and 3
>> m(:,1:3)=[]
m =
8 15
14 16
20 22
21 3
2 9
>> m([4,5],:)=[]
% eliminate row 4 and 5
m =
8 15
14 16
20 22
>>
>> m = magic(5);
>> cols_to_eliminate = [1,3,5];
>> m(:,cols_to_eliminate) = []
m =
24 8
5 14
6 20
12 21
18 2
>>
>> m = magic(5);
>> cols_to_eliminate = [1,3,5];
>> rows_to_eliminate = [2,4];
>> m(rows_to_eliminate,cols_to_eliminate) = []
A null assignment can have only one non-colon index.
That doesn't work, but
>> rows_to_keep = [2,4];
>> cols_to_keep = [1,3,5];
>> m = magic(5);
>> m = m(rows_to_keep,cols_to_keep)
m =
23 7 16
10 19 3
>>
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!