eliminate several columns and rows of a matrix using vectors.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pedro Guevara
am 9 Mai 2019
Kommentiert: Pedro Guevara
am 9 Mai 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 Kommentare
Akzeptierte Antwort
per isakson
am 9 Mai 2019
Bearbeitet: per isakson
am 9 Mai 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
>>
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!