MATLAB Answers

0

eliminate several columns and rows of a matrix using vectors.

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.

1 件の回答

回答者: 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 2019 年 5 月 9 日
thank you

サインイン to comment.



Translated by