How to convert actual frequency to normalised frequency? - matlab

I have two data sets ( which is 80*80 matrix) with relative risk ranging from -1.5 to +1.5.
I want to plot these two data sets as ine normalised frequency distribution plot.
How can I convert the actual frequency to normalised one( range 0 to 1)
So what I actually want is: if my frequency ranges from 0 to 200
( i want 0 to assign value of 0, 20 as 0.1, 40 as 0.2 , 60 as 0.3 .... 200 as 1)
So if the value of relative risk is -1 and actual frequency at this risk is 60 for dataset one and 80 for data set two, so in that case, I want -1 ( which is relative risk value) to show frequency as 0.3 and 0.4 for dataset one and two respectively after normalization. I need it in the same graph so that I could figure out the difference between two data sets.
This what I want my graphs axis to be:
Y-axis: normalized frequency for the following groups (ranging from 0 to 1)
X-axis; RR classes - <-1.5, -1.5 to -1.25, -1.25 to -1, -1 to -.75, -0.75 to -0.5, -0.5 to -0.25, -0.25 to 0, 0 to 0.25, 0.25 to 0.5, 0.5 to 0.75, 0.75 to 1, 1 to 1.25, 1.25 to 1.5 and >1.5

From the matlab documentation:
% assuming a,b are your frequencies
figure; hold on;
histogram(a,'Normalization','probability');
histogram(b,'Normalization','probability');

Related

Simulink misses data points in a from-workspace block for discrete simulation

I have a simulation running at 50 Hz, and some data that comes in at 10 Hz. I have extra 'in-between' points with dummy data at the following 50 Hz time points, and interpolation set to off. This should in theory ensure that between 10 Hz time steps, the dummy data is being held and only at the 10 Hz steps is the actual data present. For example, my data vector would be
[0.0 0.8 0.1 0.12 0.2 0.22 0.3 0.32 0.4 0.42 0.5 0.52 ...
-1 -1 1 -1 2 -1 3 -1 4 -1 5 -1 ...]
However, with a scope attached directly from the 'from-workspace' block, simulink is returning this:
[0.0 0.8 0.1 0.12 0.2 0.22 0.3 0.32 0.34 0.4 0.42 0.5 0.52...
-1 -1 -1 -1 2 -1 3 3 -1 4 -1 5 5...]
where some values are skipped and others are repeated in a consistent pattern. Is there something with simulinks time-step algorithms that would cause this?
Edit: A solution I ended up finding was to offset the entire time vector by 1/100th of a second so that the sim was taking data between points rather than on points, and that seemed to fix it. Not ideal, but functional.

2D interpolation with interp2

I want to describe surface flatness of a plane with interp2 function.
Spatial sampling points are as below.
width=[0 500];
length=[0 100 200 300 400 500 600 700 800 900 1000];
and flatness are as below, at width 0 and 500, respectively.
a = [1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1]; % flatness at width 0
b = [-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1]; % flatness at width 500
With these values, the surface shape will be like following figure.
I wanted to change this figure into following figure with interp2 function.
Below is my code.
widthq=[0 100 200 300 400 500];
flatness=[a' b'];
flatnessq=interp2(width,length,flatness,widthq,length);
But, not working with one error, 'The input data has inconsistent size.'
Can anyone explain this error and give a way how to interpolate my data with interp2 function?
The first three inputs need all to be of the same size
[W, L] = meshgrid(width, length);
The last two arguments need also to be of same size
[Wq, Lq] = meshgrid(widthq, length);
Then it should work
flatnessq=interp2(W,L,flatness,Wq,Lq);

Brute force in Matlab

this is my problem, for example i have an equation x + y =2, so using matlab i want to know how to determine all the possible combination of values of x and y when you add it, and will give sum of 2 (ex: x1 = 0.98, y1 =0.12; x2=0.94 y2=0.16, and etc)
i think i need to use for loop?
for x = 2-y
end
for y =2-x
end
Values of x and y
x y
0 2
0.1 1.9
0.2 1.8
0.3 1.7
0.4 1.6
0.5 1.5
0.6 1.4
0.7 1.3
0.8 1.2
0.9 1.1
1 1
so guys i need your help thanks
To get all possible combinations of x and y between 0 and 2 with a step size of 0.1 you don't even need a for loop. You can create a vector x which contains all possible x values and then calculate the corresponding y's:
x = 0:0.1:2; % Create a vector of values between 0 and 2 in steps 0f 0.1
y = 2 - x;
This will give you two (row) vectors containing all possible combinations which add up to 2.

How to plot two stem plots and two box plots in one figure in MATLAB?

I have two vectors
A=[0.1 0.1 0.2 0.2 0.3 0.3 0.3 0.3 0.4 0.5 0.5]
B=[0.7 0.7 0.8 0.8 0.9 0.9 0.9 0.9 1 1 1]
How to plot their stem plots and box plots at the same time?
The y-axis should be the probability of the stem plots.
What I am looking for is something like this.
Drawing the box-plots can be accomplished with
boxplot([A B], [ones(size(A)) 2*ones(size(B))], ...
'orientation', 'horizontal', 'positions', [1 1]);
After which you can add the stem plots with
hold on
stem(xa, ya);
stem(xb, yb);
where I'm not sure exactly what you are asking for for x and y.

X axis scaling with matlab plotting

My data is sparse therefore when I plot my graph I get the following result
As you can see the first x axis tick starts at 500(s), but most of my data is around 30(s). Can I change the scaling of the x axis?
How about this?
X = [1 3 6 10 25 30 235 678 1248];
Y = [0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.8 0.9];
plot(X,Y,'-b.')
figure
semilogx(X,Y,'-b.')
I see the following output:
If you want to display data from 0 to 30s only you can either plot only those like this:
idcs=Xdata <30; %# find indices where X is less than 30s
plot(Xdata(idcs),Ydata(idcs),'b'); %#plot only these data.
or you can just express XLimits on the figure.
plot(Xdata,Ydata,'b'); %# plot everything
set(gca,XLim,[0 30]); %# limit display on X axis