extract all rows of a matrix except 'r' (vector) rows
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alberto Acri
am 8 Sep. 2023
Beantwortet: MarKf
am 8 Sep. 2023
Hi! I need to extract all rows of 'matrix' except 'r' rows.
matrix = [54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65];
r = [1; 2; 3; 4: 9];
The result is:
matrix_out = [58; 59; 60; 61; 63; 64; 65];
0 Kommentare
Akzeptierte Antwort
Stephen23
am 8 Sep. 2023
The most efficient approach:
matrix = [54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65];
r = [1; 2; 3; 4; 9];
matrix_out = matrix;
matrix_out(r) = []
0 Kommentare
Weitere Antworten (2)
Dyuman Joshi
am 8 Sep. 2023
Bearbeitet: Dyuman Joshi
am 8 Sep. 2023
matrix = [54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65];
r = [1; 2; 3; 4; 9];
%Method 1
%remove directly
matrix(r,:)=[];
%or
%Method 2
%remove indirectly
idx = 1:size(matrix,1);
ix = setdiff(idx,r);
matrix = matrix(ix,:);
Note that Method 1 won't work when there is an index not in the range of the dimensions of the array matrix.
For such cases, use Method 2.
0 Kommentare
MarKf
am 8 Sep. 2023
mat = magic(8)
r_not = [1 3:5 8];
mat(r_not,:) = [] %use mat_o = mat; to keep the original mat;
There are some issues with your example:
matrix = [54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65];
r = [1:3, 6, 9]; %btw this did not work with those dimensions;
% also not the way you wanted with those indices (matrix_out just removed 1:3)
matrix_out = matrix;
matrix_out(r,:) = []
Since yours is a simple vector, matrix_out(r) = []; would have worked easily too
0 Kommentare
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!