PLEASE HELP - how to COUNT occurances
5 views (last 30 days)
Show older comments
Hello,
I have an array (sortedresults) which has 3 columns (time, land, ilok) and each contains integer values. In column 2 (land) I need to COUNT the number of times that integer changes from 3 to anything else EXCEPT zero, AND verify the value of column 3 (ilok) is either the value of 2 or 3. When both conditions are met I need to add 1 to my COUNT.
Thanks in advance!
2 Comments
Star Strider
on 19 May 2014
Please clarify: In column 2 (land) I need to COUNT the number of times that integer changes from 3 to anything else EXCEPT zero...
Does the direction of the change matter, i.e. does that only mean the change from row k to row k+1 or from row k-1 to row k, either, or both?
Gear-up landings can really take the fun out of your day! (I had to drop the gear by hand once. Fortunately it locked down.)
Accepted Answer
Star Strider
on 20 May 2014
This seems to work on the test data I’ve given it:
sortedresults = randi(5, 35, 3); % Create Data
sortedresults(:,2) = sortedresults(:,2)-1; % Allow zeros in ‘Land’
DifLand = [diff(sortedresults(:,2)); 0] % Calculate changes in ‘LAND’ state
DxLand = [(1:length(sortedresults))' sortedresults(:,2) DifLand sortedresults(:,3)] % Diagnostic check matrix
Land3ix = find((sortedresults(:,2) == 3) & (ismember(DifLand,[-2 -1 +1]))) % Find indices of changes in ‘LAND’ meeting criteria
Iloc23 = find((sortedresults(:,3) == 2) | (sortedresults(:,3) == 3)) % Find indices of ILOC meeting criteria
LandIlocTrue = intersect(Land3ix, Iloc23) % Indices of ‘LAND’ and ‘ILOC’ meeting criteria
Count = length(LandIlocTrue) % Count occurrences
I left the trailing semicolons off, so it will display the results of each step. Note that it deals with the indices of the rows of the data. All the functions are core MATLAB and do not require any Toolboxes.
The only line that is not necessary in your application code is the ‘DxLand’ matrix. The first column is the row index, the second is ‘LAND’, the third are the end-zero-padded differences in ‘LAND’ (zero-padded to make the vector equal to the others), and the fourth is ‘ILOC’. I created ‘DxLand’ so I could check to be sure the code worked. I ran the code several times to be sure it worked as I understand your criteria.
6 Comments
Star Strider
on 20 May 2014
My pleasure!
And be sure you always have ‘three in the green’ on short final!
More Answers (1)
Image Analyst
on 20 May 2014
How about this:
sortedresults = randi(5, 300, 3); % Create sample data.
% Extract columns 2 and 3.
land = sortedresults(:, 2)
ilok = sortedresults(:, 3)
% Find where ilok column has a value of 2 or 3.
ilok2or3 = ilok == 2 | ilok == 3
% Find the change from one element to the next in column 2
diffland = diff(land)
% Find where the value is 3 in land, and the difference is not 0 or -3
% and column 3 = 2 or 3.
land3 = (land(1:end-1) == 3) & (diffland ~=0 & diffland ~= 03) & ilok2or3(1:end-1)
% Sum up the result.
result = sum(land3)
2 Comments
Image Analyst
on 20 May 2014
You're welcome. It's good to scroll all the way down the screen to make sure you see all responses.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!