How to count repeated numbers and perform statistic on it?

조회 수: 2 (최근 30일)
Xiu Wen Kang
Xiu Wen Kang 2022년 11월 30일
댓글: Xiu Wen Kang 2022년 12월 2일
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 !

채택된 답변

Image Analyst
Image Analyst 2022년 11월 30일
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
Image Analyst
Image Analyst 2022년 12월 1일
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)
Xiu Wen Kang
Xiu Wen Kang 2022년 12월 2일
Thanks for your help!
I will give it a go :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by