How to count repeated numbers and perform statistic on it?

1 view (last 30 days)
Hi all,
I have a two column arrays where one represent time, the other are either 1, -1 or 0 which representing three different structures of my molecules.
for example of A = [
94.668 -1
94.67 1
94.672 -1
94.674 -1
94.676 0
94.678 1
94.68 -1
94.682 -1
94.684 -1
94.686 1
94.688 -1
94.69 1
94.692 -1
94.694 1
94.696 1
94.698 -1
94.7 -1
94.702 1
94.704 -1
94.706 -1
94.708 -1
94.71 1 ]
I want to count for example, structure(1) continuous of 2 array elements has how many of them, continuous of 3 array elements has how many of them, etc.
also the same statistic calculation for structure (-1) and structure (0).
so by looking at the above example, (-1) repeating 1 has 3, (-1) repeating 2 has 2 (-1) repeating 3 has 2, (0) repeating 1 has 1, (1) repeating 1 has 5, .....
the total length of my arays is 90000 X 2, which is large quantity of numbers to calculate manually, so may I as how can I write a matlab script to account for this problem?
Thank you !

Accepted Answer

Image Analyst
Image Analyst on 30 Nov 2022
Try this:
A = [
94.668 -1
94.67 1
94.672 -1
94.674 -1
94.676 0
94.678 1
94.68 -1
94.682 -1
94.684 -1
94.686 1
94.688 -1
94.69 1
94.692 -1
94.694 1
94.696 1
94.698 -1
94.7 -1
94.702 1
94.704 -1
94.706 -1
94.708 -1
94.71 1 ];
% Find distribution of -1 runs.
mask = A(:, 2) == -1;
props = regionprops(mask, 'Area');
areaMinus1 = [props.Area];
subplot(3, 1, 1);
histogram(areaMinus1)
grid on;
title('Distribution of -1 runs')
xlabel('Run Length')
ylabel('Count')
drawnow;
% Find distribution of 0 runs.
mask = A(:, 2) == 0;
props = regionprops(mask, 'Area');
areas0 = [props.Area];
subplot(3, 1, 2);
histogram(areas0)
grid on;
title('Distribution of 0 runs')
xlabel('Run Length')
ylabel('Count')
drawnow;
% Find distribution of 1 runs.
mask = A(:, 2) == 1;
props = regionprops(mask, 'Area');
areas1 = [props.Area];
subplot(3, 1, 3);
histogram(areas1)
grid on;
title('Distribution of 1 runs')
xlabel('Run Length')
ylabel('Count')
drawnow;
  3 Comments
Image Analyst
Image Analyst on 1 Dec 2022
You can use writetable to write the data to a text file:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 22;
markerSize = 20;
A = [
94.668 -1
94.67 1
94.672 -1
94.674 -1
94.676 0
94.678 1
94.68 -1
94.682 -1
94.684 -1
94.686 1
94.688 -1
94.69 1
94.692 -1
94.694 1
94.696 1
94.698 -1
94.7 -1
94.702 1
94.704 -1
94.706 -1
94.708 -1
94.71 1 ];
% Find distribution of -1 runs.
mask = A(:, 2) == -1;
props = regionprops(mask, 'Area');
areaMinus1 = [props.Area];
subplot(3, 1, 1);
histObjectMinus1 = histogram(areaMinus1)
grid on;
title('Distribution of -1 runs')
xlabel('Run Length')
ylabel('Count')
drawnow;
% Find distribution of 0 runs.
mask = A(:, 2) == 0;
props = regionprops(mask, 'Area');
areas0 = [props.Area];
subplot(3, 1, 2);
histObject0 = histogram(areas0)
grid on;
title('Distribution of 0 runs')
xlabel('Run Length')
ylabel('Count')
drawnow;
% Find distribution of 1 runs.
mask = A(:, 2) == 1;
props = regionprops(mask, 'Area');
areas1 = [props.Area];
subplot(3, 1, 3);
histObject1 = histogram(areas1)
grid on;
title('Distribution of 1 runs')
xlabel('Run Length')
ylabel('Count')
drawnow;
% Export histogram to a file. Here just doing the last histogram but you can do it for all 3 of them if you want.
outputFileName = 'delete me.txt'; % Or 'hist1.csv' or whatever you want.
t = table(histObject1.BinEdges(1:end-1)', histObject1.Values',...
'VariableNames', {'LeftBinEdge', 'Count'})
writetable(t, outputFileName);
winopen(outputFileName)

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!