How to draw a Histogram in Matlab - matlab

I have a set of around 35000 data. These data are the signal strengths received only from a single location for different time interval of time. I want to plot a Histogram using these data. My X-axis will give the information about "Signal Strengths" and my Y-axis will give the information about "Probability". My histogram will consists of different bars which will give information about the signal strength and probabilities.
For example, suppose I have the following data
a= [ 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 5 6 6 6 6 6 6 6 6 6 6 6]
How can I plot the graph using data at X-axis and Probability at Y-axis? Any help will be appreciated. Thanks!

This should work just fine if you don't want to use some predefined functions:
una=unique(a);
normhist=hist(a,size(unique(a),2))/sum(hist(a));
figure, stairs(una,normhist)
Una has only the unique values of a, normhist is now between 0 and 1 and it's the probability of occurring of the individual signal because you divide it by the number of elements included in the data.

Related

Histogram rebinning in Matlab 2020a, when I don't have a Matlab histogram object

I have two vectors that describe a histogram: bin values/labels and bin count.
I'd like to import those into a Matlab histogram object so that I can change parameters more easily, such as the number of bins.
Here is a simplification what I have:
Center bin values:
BinValues(1:21) = [-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10];
and count values per bin
CountValues(1:21) = [ 16 12 3 21 7 8 2 6 0 3 4 6 1 8 3 25 16 5 7 10 16];
My actual histogram has 800000 bins, and I want to vary the number of bins.
In my simplified example above, it would be something like reducing the bins from 21 to 15 or 21 to 11.
What I have can be graphed as a simple bar graph.
bar(BinValues,CountValues);
But, if I want to experiment with different numbers of bins, I think it would be best to use the histogram function/object in Matlab, but I'm not sure how I do this from what I have.
OK Update:
I tried this:
h=histogram('BinEdges',[-10.5:10.5] , 'BinCounts', CountValues);
Now I have a nice histogram, but if I use fewerbins(h), I get an error saying I can't do that while BinCountsMode is in "manual". If I modify it to be in "auto" mode, then my original histogram disappears.
The brute force solution would be to generate a huge array with repeated values as dictated by binCount values, but that seems like a dumb way to solve this problem.

extract the unique blocks from matrix using Mean Square Error matlab

The Mean Square Error(MSE), is a method used to define the difference in between two blocks, and can be calculated as follow: a and b two blocks equal size
MSE = sqrt(sum(sum((a-b).^2)))/size(a or b)
If the MSE is less than a given threshold, than the two blocks are not different.
Given a matrix A, already reshaped to be contain blocks all in the same raw,
the purpose is to extract all blocks where the MSE is less than a given threshold (based on the first block), then return the mean of those blocks. again, extract the second group of blocks which the MSE is less than the given threshold where the blocks that already assigned to be a part of other group of blocks must not be extracted again. Better than that, it must be deleted to reduce the search time. and so on till all blocks of the matrix A are assigned to be part of a group. the blocks of the resulted matrix should be organized based on the number of block within the group, from the biggest number of blocks to the lowest. And here is an example :
Given matrix A where the size of A is 2 by 14:
A= [1 1 2 2 9 9 4 4 6 6 5 5 3 3
1 1 2 2 9 9 4 4 6 6 5 5 3 3];
PS: its not necessary the blocks contain the same numbers, it is just to make the example clear.
blocks size is : 2 by 2
the threshold is 2
now we extract all blocks where the MSE is less than the threshold starting from the first block in the matrix A. so the blocks are:
1 1 2 2 3 3
1 1 2 2 3 3
the mean of those blocks is
Result= [ 2 2
2 2];
again. we extract all blocks where the MSE is less than the threshold, but we need to avoid the blocks that already extracted, so the second group of blocks is :
9 9
9 9
the mean of this block is it self, so:
Result= [2 2 9 9
2 2 9 9];
again. we extract all blocks where the MSE is less than the threshold, but we need to avoid the blocks that already extracted, so the third group of blocks is :
4 4 6 6 5 5
4 4 6 6 5 5
the block
3 3
3 3
is not a part of this group even if the MSE is less then the threshold because is already extracted to be part of the first group.
the mean of those blocks is:
5 5
5 5
therefore the result should be:
Result= [2 2 5 5 9 9
2 2 5 5 9 9 ];
there are any fast way to apply that ?
PS: that Datasize is huge, therefore , there are a need for a fast way to do that.

Plot multiple data matlab [duplicate]

This question already has answers here:
Show two different plots in one plot
(2 answers)
Closed 8 years ago.
Good Morning, I have a problem with matlab plot.
I have generated sample of data that belong to different days; the data are the main posture of the human (labelled with 1,2,3,4).
Now I have 30 vector (one for each day) with the number of sample equals to the seconds of the day (about 86400 sample...). I have one posture for each second.
My aim is to plot the distribution of the sample during one month, in X axis I would have the days of the month (1,2,3.....30) and in the Y axis I would have the hour (sample/3600 I think).
How can I plot all the data in only one graph? I have two main problem:
I have 30 vector with different lenght (because I have generated the sample with random function) so the first step is to allineate the data I think because PLOT function needs vectors with the same lenght...
plot 30 days in the same plot, in order to evaluate the whole distribution of the posture in a month
A small example: day1 = [2222111333444] day2 = [22111333333444] day3 = [2221111133334444]. The input are sequences of postures (one sequence for day); now I need to obtain a plot with a "vertical representation" of these postures (on the x axis the days, on the y axis the hour of the day, for each hour I have about 3600 sample-one sample for second). With the command "hold on" no problem but I don't need to overlap the data but I need to place side by side the vector data
Andrea
It goes something like this, but of course,if you have 30 days and one entry per second you would need to use a matrix and sum the individual rows. Also, you don't need to make the vectors the same size, but then you have to use a different parameter for the x axis (Days) everytime.
day1=[2 2 2 2 1 1 1 1 3 3 3 4 4 4];
day2=[2 2 1 1 1 3 3 3 3 3 4 4 4 4];
day3=[2 2 2 1 1 1 1 3 4 4 4 4 4 4];
Days=1:3;
LayingTime=[sum(day1==1),sum(day2==1),sum(day3==1)];
SittingTime=[sum(day1==2),sum(day2==2),sum(day3==2)];
StandingTime=[sum(day1==3),sum(day2==3),sum(day3==3)];
RockingTime=[sum(day1==4),sum(day2==4),sum(day3==4)];
plot(Days,LayingTime,Days,SittingTime,Days,StandingTime,Days,RockingTime)
xlabel('Day')
ylabel('Hours of Activity')
legend('Hours Laying','Hours Sitting','Hours Standing','Hours Rocking')

Set x-axis to non-monotonic values and avoid scaling

I use matlab to plot a graph where instead of having x-axis increase monotonically, I have my own values. eg 5 14 8 9 12 7 etc.I use set (gca,'XTickLabel',num2str(mydata)) which generally works. However, when mydata is more than four or five digits, Matlab scales the graph and thus x-axis values no longer correspond to their intended points. Any ideas on how to prevent this scaling? To clarify, when I make the figure larger, it shows the plot as it should.
The problem is in your num2str() conversion:
mydata = 1:10;
num2str(mydata)
ans =
1 2 3 4 5 6 7 8 9 10
This means, that each tick will be labelled with this long 1 by n char array. The axes will then be resized to fit the labels inside the figure.
A solution is to create one label per row of a char array:
reshape(sprintf('%2d',mydata),2,[])'
ans =
1
2
3
4
5
6
7
8
9
10
Sort of solution is to write set(gca,'xtick',1:myDataVectorLength) before set (gca,'XTickLabel',num2str(mydata))

Scramble an nx1 matrix in matlab efficiently?

I need to randomly scramble the values of an nx1 matrix in matlab. I'm not sure how to do this efficiently, I need to do it many times for n > 40,000.
Example
Matrix before:
1 2 2 2 3 4 5 5 4 3 2 1
Scrambled:
3 5 2 1 2 2 3 4 1 4 5 2
thank you
If your data is stored in matrix data, then you can generate "scrambled" data using randperm like so:
scrambled = data(randperm(numel(data)));
This is sampling without replacement, so every value in data will appear once in scrambled.
For sampling with replacement (values in data may appear in scrambled multiple times and some may not appear at all), you could use randi like this:
scrambled = data(randi(numel(data),1,numel(data)));