필터 지우기
필터 지우기

remove row from matrix

조회 수: 1 (최근 30일)
Jakob Hannibal
Jakob Hannibal 2014년 10월 31일
댓글: Jakob Hannibal 2014년 11월 4일
Hello!
I have a problem where I need to remove som rows from a matrix. I need to remove all numbers under 10 and over 60 from the first row and all negative numbers from the second row. The third row has to be between 1:4. I need to print if a row has been removed, so I know which rows are missing, which I haven't implemented in the code below. I have tried in many ways and has come up with this that removes the right rows, but I am sure it can be done in a smarter way…
clc;clear; false_data = [8 20 15 35 65 35; 0.1090 -0.50 0.5170 1.0860 0.9340 0.1090;1 2 3 4 6 1]'; approved_bacteria = [1 2 3 4];
for i = 1:length(false_data(:,1)) indices = 10 > false_data(:,1) | (false_data(:,1) > 60)
false_data(indices,:) = []; end
for i = length(false_data(:,2)) indices = false_data(:,2) < 0; false_data(indices,:) = []; end
for i = length(false_data(:,3)) indices = false_data(i,3) < 1 false_data(i,3) > 4; false_data(indices,:) = []; end false_data

채택된 답변

Sean de Wolski
Sean de Wolski 2014년 10월 31일
This is how I'd do it.
% Bounds
lb = [10 0 1];
ub = [60 inf 4];
% Simulate data
n = 1000;
data = [randi(100,[n,1]) randn(n,1) rand(n,1)*10];
% Apply conditions
idxrmv = any(bsxfun(@lt,data,lb) | bsxfun(@gt,data,ub),2);
% Remove
data(idxrmv,:) = [];
% print
fprintf('Removed row %3.0i\n',find(idxrmv));
  댓글 수: 5
Sean de Wolski
Sean de Wolski 2014년 10월 31일
loop over the rows of the matrix, find the columns in each row, print.
Jakob Hannibal
Jakob Hannibal 2014년 11월 4일
When I run the function I get this errormessage:
Enter the filename to load: data0.txt
data =
25.0000 0.1090 1.0000
20.0000 0.0960 2.0000
100.0000 0 3.0000
15.0000 0.8000 5.0000
35.0000 1.0860 4.0000
40.0000 0.9340 2.0000
35.0000 0.1090 1.0000
22.0000 0.1000 1.0000
21.0000 0.1000 2.0000
16.0000 0.6000 3.0000
17.0000 0.8500 5.0000
32.0000 1.0800 4.0000
45.0000 0.9500 2.0000
37.0000 0.1100 1.0000
Error using bsxfun Non-singleton dimensions of the two input arrays must match each other.
Error in checkdata (line 10) idxrmv = any(bsxfun(@lt,data,lb) | bsxfun(@gt,data,ub),2);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by