How to find how many times a particular number occurred in large random sequence and their respective occurence position

2 views (last 30 days)
I am dealing with this problem and after googling I found it shall be better to post here. I have long 1000 numbers of random sequence.Each data size should not exceed 8bit so in decimal it be up to 255.
So I need to generate a code which shall tell how many times a particular number and also where thy occurred.I did not find any way to hold their position information.I am at beginner stage with matlab. so my output shall be that 255 occurs 3 times and in 1st ,108th and 500 th position.How to find this position values?
any lead shall be highly appreciated
thank you in advance
regards
Joy
  1 Comment
Matt Kindig
Matt Kindig on 19 Oct 2012
Edited: Matt Kindig on 19 Oct 2012
I assume since your data is 8-bit that the values can only be integers from 0 to 255. One way:
%suppose R is your data, of random integers from 0 to 255
D = 0:255; %possible values
L = arrayfun(@(x) find(ismember(R,x)), D, 'UniformOutput', false);
%each element of L is the indexes of where the number 0, 1, 2, etc. are found
Counts = cellfun(@length, L, 'UniformOutput', true);
%each element of Counts is the number of matches
% for the particular case of finding the matches for 255:
number255 = Counts(D==255);
locations255 = L{D==255};

Sign in to comment.

Accepted Answer

Matt J
Matt J on 19 Oct 2012
locations=find(A==255);
howmanytimes=length(locations);

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 19 Oct 2012
Edited: Azzi Abdelmalek on 19 Oct 2012
data=randi(2,1000,8)-1;
v=[0 1 1 0 0 0 1 0];
idx=find(~any(cell2mat(arrayfun(@(x) data(x,:)-v ,1:size(data,1),'un',0)'),2))

Image Analyst
Image Analyst on 19 Oct 2012
Joy: Try this demo. Don't be afraid. Just copy, paste, and run. After that you can go back and read the comments to understand what it does. Adapt to your situation as needed.
% Goes through every 8th graylevel from 0 to 255
% showing which pixels have that graylevel.
% A high contract binary image (like a map) of where they live is shown.
% This is then used as a mask to extract out an image showing
% only those pixels in their original gray level, with all other pixels black.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 12;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(1, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0.5 1 .5]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Now go through every 8th gray level showing where it is.
for gl = 0 : 8 : 255
% Get a logical image (true/false) showing where the pixels are.
pixelMap = grayImage == gl;
subplot(1, 3, 2);
imshow(pixelMap);
caption = sprintf('Pixel Map\nBinary location of pixels\nwith gray level %d', gl);
title(caption, 'FontSize', fontSize);
% Display where they are located in the command window.
fprintf('For gray level = %d, the rows and columns are:\n', gl);
[rows columns] = find(pixelMap)
% Mask the image to show the image pixels with their actual gray
% levels.
maskedImage = bsxfun(@times, grayImage, cast(pixelMap, class(grayImage)));
subplot(1, 3, 3);
imshow(maskedImage);
caption = sprintf('Masked Image\nThese are the actual pixels with\ngray level %d extracted and shown in their own gray level.', gl);
title(caption, 'FontSize', fontSize);
% Ask user if they want to continue.
promptMessage = sprintf('Showing pixels with gray level = %d\nDo you want to Continue processing,\nor Cancel to abort processing?', gl);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
return;
end
end

Community Treasure Hunt

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

Start Hunting!