MATLAB Answers

eliminate several columns and rows of a matrix using vectors.

3 ビュー (過去 30 日間)
Pedro Guevara
Pedro Guevara 2019 年 5 月 9 日
コメント済み: Pedro Guevara 2019 年 5 月 9 日
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

  0 件のコメント

サインイン to comment.

採用された回答

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
>>

  1 件のコメント

Pedro Guevara
Pedro Guevara 2019 年 5 月 9 日
thank you

サインイン to comment.

その他の回答 (0 件)

サインイン してこの質問に回答します。


Translated by