Borrar filtros
Borrar filtros

how to delete a row in a cell array

7 visualizaciones (últimos 30 días)
gmltn1212
gmltn1212 el 4 de Jul. de 2020
Respondida: Image Analyst el 4 de Jul. de 2020
Hi I am trying to delete rows depending on the two lowests rows in a column...
A = {'rowcount' 'one' 'two' 'three'; 'rowone' [1] [2] [3]; 'rowtwo' [10] [11] [12]; 'rowthree' [7] [8] [9]; 'rowfour' [4] [5] [6];}
based on the third column, I want to delete the lowest two rows and return this
B = {'rowcount' 'one' 'two' 'three'; 'rowtwo' [10] [11] [12]; 'rowthree' [7] [8] [9];}

Respuestas (2)

Walter Roberson
Walter Roberson el 4 de Jul. de 2020
[~,idx] = sort(cell2mat(A(2:end,3)));
A(idx(1:2)+1,:) = [];

Image Analyst
Image Analyst el 4 de Jul. de 2020
Since your question is ambiguous, I've done it for you both ways:
A = {'rowcount' 'one' 'two' 'three'; 'rowone' [1] [2] [3]; 'rowtwo' [10] [11] [12]; 'rowthree' [7] [8] [9]; 'rowfour' [4] [5] [6];}
B = {'rowcount' 'one' 'two' 'three'; 'rowtwo' [10] [11] [12]; 'rowthree' [7] [8] [9];}
% Remove rows based on 2 lowest values in column 3 (like you said)
col3 = [A{2:end, 3}];
[sortedCol3, sortOrder] = sort(col3, 'ascend');
B = A; % Initialize
B(sortOrder(3:end),:) = [] % Remove rows 2 and 5 by setting them to null.
% Remove rows based on 2 lowest values in column 4
% (like you showed in your example, contrary to what you said.)
col4 = [A{2:end, 4}];
[sortedCol4, sortOrder] = sort(col4, 'ascend');
B = A; % Initialize
B(sortOrder(3:end),:) = [] % Remove rows 2 and 5 by setting them to null.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by