Getting different gray-level values from a matrix (Gray-level image) - matlab

I have this matrix:
image= [200 200 200 200 200 200 200;
200 180 180 180 180 180 200;
200 180 120 150 120 180 200;
200 180 150 40 150 180 200;
200 180 120 150 120 180 200;
200 180 180 180 180 180 200;
200 200 200 200 200 200 200];
I want to get the different gray-values and put them in an array, so my array will look like this:
Array= [40 120 150 180 200];
How can I do this in MATLAB?

The unique function does exactly what you are looking for.

Related

How can I plot heatmap of Wi-Fi signal strength in a 2-D surface?

I've my dataset as follows (for instance):
strength = [-90 -90 -90 -90 -40 -20 -22.4 -45 -35 -41 -44 -55 -40 -75 -26]
X = [10 550 550 10 50 234 393 129 237 328 448 225 344 457 477]
Y = [10 10 410 410 293 210 202 132 130 142 141 272 268 274 200]
Here, strength is the received signal strength in dBm, X and Y are 2-D coordinates. I want to plot the heatmap of the signal strength at all the coordinate points.
My situation is similar to this question. However, I want to plot the received signal strength and in Matlab, as shown in the attached figure (because of picture quality). A solution in Matlab is found here, however, the code given in the link does not work. The Command Window of Matlab displays HeatMap object with 0 rows and 0 columns. The available code in Matlab is as follows:
strength = [-90 -90 -90 -90 -40 -20 -22.4 -45 -35 -41 -44 -55 -40 -75 -26]';
X = [10 550 550 10 50 234 393 129 237 328 448 225 344 457 477]';
Y = [10 10 410 410 293 210 202 132 130 142 141 272 268 274 200]';
strengthPercent = 2*(strength+100)/100;
picture = imread('https://www.mathworks.com/help/examples/thingspeak/win64/CreateHeatmapOverlayImageTSExample_02.png');
[height,width,depth] = size(picture);
OverlayImage=[];
F = scatteredInterpolant(Y, X, strengthPercent,'linear');
for i = 1:height-1
for j = 1:width-1
OverlayImage(i,j) = F(i,j);
end
end
alpha = (~isnan(OverlayImage))*0.6;
imshow(picture);
hold on
OverlayImage = imshow( OverlayImage );
caxis auto
colormap( OverlayImage.Parent, jet );
colorbar( OverlayImage.Parent );
set( OverlayImage, 'AlphaData', alpha );
Any suggestions, please!
Thank you.
You can use scatteredInterpolant as in the code you showed, just pair it with surf
F = scatteredInterpolant(X(:), Y(:), strength(:));
[XD, Yd] = meshgrid( 0:10:410, 0:10:550 );
Zd = F( XD, Yd );
surf( XD, Yd, Zd, 'EdgeColor', 'interp' );
view([0,90]); % view 3D plot from above
You can change the colormap to customise the appearance.
colormap( 'hot' );
colorbar();
You should be able to reduce the FaceAlpha of the surface to lay it over an image.

How to create a sub-matrix in MATLAB

I have this work which I have to do by creating a sub-matrix out of a given data set. I will explain it below.
Suppose, I have the data set as:
100 200 300 400 500 600
101 201 301 401 501 601
102 202 302 402 502 602
So, I want to create sub-matrices as follows:
For the first iteration ->
[[101 201 301 401 501]
[102 202 302 402 502]]
and
[[601]
[602]]
For the second iteration ->
[[100 200 300 400 500]
[102 202 302 402 502]]
and
[[600]
[602]]
And so on... The process will continue till the number of rows in the main/starting matrix.
In short, I want a LOO (leave one out) implementation of this data set, so that I can further work on it.
If you guys have any idea on how to do it, please share it. :)
Assuming A is the main matrix, a1 and a2 will be your first set of sub-matrices and b1 and b2 will be the second set of sub-matrices.
>> A=[100 200 300 400 500 600
101 201 301 401 501 601
102 202 302 402 502 602];
>> a1=A(2:3,1:5)
a1 =
101 201 301 401 501
102 202 302 402 502
>> a2=A(2:3,6)
a2 =
601
602
>> b1=A(1:2,1:5)
b1 =
100 200 300 400 500
101 201 301 401 501
>> b2=A(1:2,6)
b2 =
600
601
A proper indexing is your friend here. For the given matrix:
X = [
100 200 300 400 500 600
101 201 301 401 501 601
102 202 302 402 502 602
];
The first subsets are:
S1A = X(2:3,1:end-1);
S1B = X(2:3,end);
and the second subsets are:
S2A = X(1:3,1:end-1);
S2B = X(1:3,end);
Since you want to perform this for all the two-rows combinations of the matrix, the rows indexing patten can be generated with the nchoosek function as follows:
X_seq = 1:numel(x);
idx = nchoosek(X_seq,2);
Then, with an iteration (just to keep things simple... althrough in Matlab is always recommended to vectorize as many computations as possile), you can extract all the matches:
idx_len = size(idx,1);
res = cell(idx_len,2);
for i = 1:idx_len
idx_curr = idx(i,:);
res(i,:) = {X(idx_curr,1:end-1) X(idx_curr,end)};
end
If you have the stats and ML toolbox then you can use their built-in cross validation functions. See cvpartition or crossvalind

Calculate Variance of a Group data

I have a table contain height and frequency.I want to calculate the variance of it.
Height 140 150 160 170 180 190
Frequency 3 5 57 63 30 2
I have tried the below code:
height=[140 150 160 170 180 190;3 5 57 63 30 2]
height=height(:)
V = var(height) %Calculate Variance
**This give an answer of 5.7316e+03**
while with formula it give an answer of 81.8594. Now please tell me how can i do this?
Use weighted variance:
h=height;
var(h(1,:),h(2,:))

Normalizing data and inverse

I use the following code to normalise my data in MATLAB:
Data=[130 100 100 100 300 300 30 300 30 320 200 50 300 25 100 250 1200 300 320 300 100 100 170 500 1000 200 120 450 200 2100 100 100 100 3450 500 30 100 550 4000 150 500 380 400 4000 180 540 700 100 500 2300 200 200 50 2000 400 100 50 50 100 4000 4000 3250 100 100 100 100 3300 100 100 4020 150 150 2300 3000 100 50 100 50 100 200 2000 300]
Data=randn(100,1);
%Histogram with histfit
nbins=20;
h=histfit(Data,nbins);
[nelements,bincenters]=hist(Data,nbins);
hold on %histogram computed manually and scaled with trapz()
avg=mean(Data);
stdev=std(Data);
x=sort(Data);
y=exp(-0.5*((x-avg)/stdev).^2)/(stdev*sqrt(2*pi));
plot(x,y*trapz(bincenters,nelements),'g','LineWidth',1.5)
legend('Histogram','Distribution from hisfit','Distribution computed manually')
It works well, I found the mean and standard deviation. Now, I try to inverse the y values with ln to find back the original points, but, I was not successful. Could you please give any idea to inverse y values?

How to write matrices from matlab to .xlsx with special formatting tables

I have one problem with exporting matrices from Matlab to Excel. This is not a problem, but I need some formatting.
I made matrices A and B and I printed them to .xlsx document.
filename = 'example.xlsx';
A;
sheet = 1;
xlRange = 'A9';
xlswrite(filename,A,sheet,xlRange)
B;
xlRange2= 'B9';
xlswrite(filename,B,sheet,xlRange2)
And i get the example.xlsx file with this formating:
400 4.56
500 5.12
600 6.76
700 7.98
800 8.21
900 9.21
1000 10.12
1100 11.23
1200 12.43
1300 13.89
1400 14.54
1500 15.21
1600 16.23
1700 17.53
I need this kind of formating:
400 4.56
500 5.12
600 6.76
700 7.98
800 8.21
900 9.21
1000 10.12
100 11.23
200 12.43
300 13.89
400 14.54
500 15.21
600 16.23
700 17.53
Steps are on 500, 1000, 1500, 2000, 2500... How to put one empty row and how to make this kind of formating?
This code provides the cell as required for xlswrite:
M=[400 4.56
500 5.12
600 6.76
700 7.98
800 8.21
900 9.21
1000 10.12
1100 11.23
1200 12.43
1300 13.89
1400 14.54
1500 15.21
1600 16.23
1700 17.53
900 9.21
1000 10.12
1100 11.23
1200 12.43
1300 13.89
1400 14.54
1500 15.21
1600 16.23
1700 17.53];
gaps=[500, 1000, 1500, 2000, 2500];
%calculates a group indx. 0 is below first gap, 1 between first and second etc..
group=sum(bsxfun(#ge,M(:,1),gaps),2);
%whenever group increases a line must be jumped, calculate indices
index=cumsum(ones(size(M,1),1)+[0;diff(group)>0]);
%allocate empty cell
X=cell(max(index),size(M,2));
%fill data
X(index,:)=num2cell(M);
xlswrite('a.xlsx',X)