How to create an indexing program that removes numbers between values
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Andrew Yeo
le 25 Juil 2017
Modifié(e) : Andrei Bobrov
le 28 Juil 2017
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]
0 commentaires
Réponse acceptée
Harish Saranathan
le 25 Juil 2017
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
Plus de réponses (1)
Andrei Bobrov
le 26 Juil 2017
Modifié(e) : Andrei Bobrov
le 28 Juil 2017
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);
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!