eliminate several columns and rows of a matrix using vectors.

2 次查看(过去 30 天)
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.
Eliminar.png

采纳的回答

per isakson
per isakson 2019-5-9
编辑:per isakson 2019-5-9
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
>>

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by