フィルターのクリア

How to create an indexing program that removes numbers between values

1 回表示 (過去 30 日間)
Andrew Yeo
Andrew Yeo 2017 年 7 月 25 日
編集済み: Andrei Bobrov 2017 年 7 月 28 日
I have a large data set of numbers that I would like to import so I need help writing a program that can do the following.
1. Find the first negative number within the set. 2. Once first negative number is found, note the positive number that came right before the first negative number found, and remove all data entries until that positive number is found again.
Some examples are...
test1 = [9,-7,7,1,10,-9,9] would print newtest1 = [9 9]
or test2 = [1, 2, 4, -2, 7, 8, 4] would print newtest2 = [1, 2, 4, 4]

採用された回答

Harish Saranathan
Harish Saranathan 2017 年 7 月 25 日
Hello Andrew,
You can accomplish your task by using logical indexing and successively calling "ismember" and "find" functions. The documentation for these functions can be found here:
The documentation for logical indexing can be found here:
The idea is to find the index of the first negative number in the array "test". From that, you can determine the index of the previous (positive number), ind1. You can then use "ismember" and "find" functions to determine the index, ind2, of the next occurrence of the positive number. After that, you can concatenate the two arrays "test(1:ind1)" and "test(ind2:end)". You can then repeat this process on the new array until all negative numbers are weeded out.
The code that implements this can be found below:
close all
clear all
clc
testOriginal = [9,-7,7,1,10,-9,9,-3,7,1,10,-9,9];
test = testOriginal;
if test(1)<0
% If the first element is a negative number, return an empty array.
test = [];
else
% Find all the negative numbers.
negativeNumber = test(test<0);
% The while loop will be entered only if negative numbers are present.
while ~isempty(negativeNumber)
% Find the index of the first negative number in the updated array
% test.
indNegativeNumber = find(ismember(test, negativeNumber(1)), 1);
% Determine the positive number that appears before the first
% occurrence of the negative number.
previousPositiveNumber = test(indNegativeNumber - 1);
% Determine the index of the next occurrence of that positive
% number in test(indNegativeNumber:end).
indNextPositiveNumber = find(ismember(test(indNegativeNumber:end), previousPositiveNumber), 1);
if ~isempty(indNextPositiveNumber)
% Determine the index of this positive number in test.
indNextPositiveNumber = indNextPositiveNumber + indNegativeNumber - 1;
% Concatenate the matrices.
test = [test(1:indNegativeNumber - 1) test(indNextPositiveNumber:end)];
else
% Case when there is no next occurrence of the positive number.
test = test(1:indNegativeNumber - 1);
end
% Find all the negative numbers in the updated array test.
negativeNumber = test(test<0);
end
end
testOriginal
test
Executing the above code results in the following output:
testOriginal =
9 -7 7 1 10 -9 9 -3 7 1 10 -9 9
test =
9 9 9
Harish
  1 件のコメント
Andrew Yeo
Andrew Yeo 2017 年 7 月 26 日
Thank you very much Harish Saranathan.
The annotations were very helpful.
I want to ask for you for advice about an adjustment to the code that I would like to make.
The data that I would like this code to 'filter' comes with a corresponding row of data, the entries of which I wish to be deleted along with its corresponding filtered entry.
An example is as follows
Inputing testOriginal = [1 2 3 4; 4 -2 5 4]
outputs test = [1 4; 4 4]

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 7 月 26 日
編集済み: Andrei Bobrov 2017 年 7 月 28 日
ii = find(test2 < 0,1 , 'first')-1;
out = [test2(1:ii-1),test2(test2 == test2(ii))];
after last Andrew's comment
t = cumsum(testOriginal(2,:) < 0) == 0;
out = testOriginal(:,testOriginal(2,:) == testOriginal(2,find(t,1,'last')) | t);

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by