label over specific cells (index) in imagesc - matlab

I have created this map of Jamaica using matrix A. I want to insert text labels on this image at specific indexes for cities. For Example kingston on this map is at point 15, 38, where 15 is the row and 38 the column, this point I would label "kingston". My matrix is below and the image of it generated from imagesc is below as well. I was playing around with get(gca, 'position') but that was not successful.
% Cities
kingston = [15 38];
montegoBay = [4 15];
portRoyal = [18 31];
stThomas = [10 55];
mandeville = [13 21];
ochoRios = [2 29];
A = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 3 3 0 0 0 0 3 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 3 0 0 0 0 3 0 3 3 3 0 0 0 0 3 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 3 3 3 0 3 0 3 3 3 3 0 3 0 3 0 3 3 3 0 3 3 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 3 3 3 0 0 0 3 3 3 3 3 3 0 3 0 3 0 0 0 0 0 0 3 0 0 0 3 1 1 1 1 1 1 1 1 1 1 1 1 1;
1 1 1 0 0 3 3 0 0 0 3 3 3 3 3 0 3 3 3 3 0 0 0 0 0 0 0 0 0 3 0 0 0 3 3 3 0 3 3 3 3 0 0 3 0 0 1 1 1 1 1 1 1 1 1 1;
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 3 3 3 0 0 3 3 0 3 0 0 0 0 3 3 3 3 0 0 0 0 0 0 0 0 1 1 1 1 1 1;
1 0 3 0 3 3 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 0 3 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
Cmap = [1 1 1; 0 0 1; 0 1 0];
colormap(Cmap);
imagesc(A);
axis off

You should use the function text like this:
text(38,15,'kingston')
i.e. in opposite order, because your axis is in i-j direction, and not x-y (try typing axis xy to see what I mean).
(I have changed the font size to 15: text(38,15,'kingston','FontSize',15))
If you want to go a step further, define your cities as a cell array:
Cities = {'kingston','montegoBay','portRoyal','stThomas','mandeville','ochoRios'};
and their location in a matrix:
location = [15 38;
4 15;
18 31;
10 55;
13 21;
2 29];
And then all you need is one text command:
text(location(:,2),location(:,1),Cities,'FontSize',12)
to get the final result:

You can use text function to do that as:
text(x,y,'MyText')
See the documentation for more info:
https://www.mathworks.com/help/matlab/ref/text.html
Hope it helps.

Related

What matlab command, or combination of commands (using 25 characters or less), could be used to create the following matrix?

What matlab command, or combination of commands (using 25 characters or less), could be used to create the following matrix?
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
Hint: Look for a lower triangluar matrix that is repeated many times. First try to produce that lower triangular matrix with as few characters as possible.
You can use the following code:
A = ones(5); % create 5x5 matrix with all elements 1
B = tril(A); % return the lower triangle matrix of A
C = repmat(B, 3, 2); % repeat the matrix B, 3 times in a row and 2 times in a cloumn as you want.
In more details:
A = ones(5);
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
B = tril(A);
1 0 0 0 0
1 1 0 0 0
1 1 1 0 0
1 1 1 1 0
1 1 1 1 1
C = repmat(B, 3, 2);
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
A solution using rempat and implicit expansion:
repmat(1:5<(2:6)',3,2)

How to use points data plot a colorful area?

I have an array, data, in MATLAB just like following:
data = [0 0 0 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1;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 0 1 1 1 1;
0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 1 1 1 1;
0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 1 1 1 1;
0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 1 1 1];
Numbers in this matrix stand for color. For example:
1 → red
0 → white
it not must be these colors, anyway,
different number → different color
I want to show on a two-dimensional plane.
I use for environment plot points (but these are discrete points, not a region).
If I use surf(data), it will have a 3D plot:
I can rotate this 3D plot and view above, just like:
This image is what I want plot on 2D plane.
Is there any way to plot on a 2D plane directly?
You may want a contour plot. You can get this by:
[X, Y] = meshgrid(1:size(data,2), 1:size(data,1));
contourf(X,Y,data);
For a more square and distinct look you could try:
scatter(reshape(X,[],1), reshape(Y,[],1), 1200, reshape(data,[1],1), 'square', 'filled');
axis equal
However, this solution requires that you to trial-and-error the size (1200) which will be dependent on the screen size of your plot.
Finally, in newer Matlab versions (R2017b+) you may use heatmap for these types of plots (https://se.mathworks.com/help/matlab/ref/heatmap.html) E.g.:
heatmap(data, 'CellLabelColor', 'none')

MATLAB: Identify maxima and minima and split data accordingly

I have the following arrays:
x = [1:33];
y = [0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 1 0 1 2 3 4 3 2 1 0];
I need to separate y into different sections. I need to obtain the ascending parts, the descending parts, and the combined ascending and descending parts.
For example:
Ascending parts = [1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 0 0 1
1 1 1 0 0 0 0];
Descending parts = [0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1
1 1 1 1 0 0 1 1 0 0 0 0 1 1 1 1];
Combined parts = [1 1 1 1 1 1 1 1 1
1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 4 4 4 4 4 4 4 4];
I can, of course, do this manually for these arrays, but I need to do this for arrays with hundreds of thousands of points and I wish to do it automatically. I have been playing around with the findpeaks functions but this isn't straightforward as it sometimes picks up peaks during the descending/ascending parts, rather than at the end points.
Any tips on how I can do this?
version MATLAB 2017/b
For a problem like this, you should consider using Matlab's diff.
For y = [0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 1 0 1 2 3 4 3 2 1 0];
a=diff(y)
1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 -1 1 1 -1 -1 1 1 1 1 -1 -1 -1 -1
b=a, c =a;
b(b<0)=0;
c(c>0)=0;
Will give you:
b = 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 0 0 1 1 1 1 0 0 0 0
c = 0 0 0 0 0 -1 -1 -1 -1 -1 0 0 0 0 0 -1 -1 -1 -1 -1 0 0 -1 -1 0 0 0 0 -1 -1 -1 -1
For the second part, you can do the following:
z=diff(y, 2);
zd=[0 find(z~=0);0 z(find(z~=0))]
0 5 10 15 20 22 24 28 32
0 -2 2 -2 2 -2 2 -2 2
Assuming that this is how your function looks like in general, the above pattern shows convex and concave regions in the sequence. With this assumption, the following should work in your case:
za=[0 zd(1,zd(2,:)>0)];
zad=diff(za);
cell2mat(arrayfun(#(x,y) repelem(x,y), 1:length(zad),zad,'UniformOutput',false))
ans: 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 4 4 4 4 4 4 4 4
which I believe is close to what you want. Hope this helps.

Using 25 characters or less to create a specific matrix in MATLAB

What matlab command, or combination of commands (using 25 characters or less), could be used to create the following matrix?
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
I got as far as this;
repmat(tril(ones(3,3)),5)
But repmat creates a 5 by 5 matrix. I however, need a 4,5 matrix.
Thank you for taking the time to help!
Add one more argument to repmat and remove one from ones (as Divakar noted):
repmat(tril(ones(3)),4,5)
As you can see, you can specify how many replications you want for both the rows and the columns. A single value argument to either function will use that value for both rows and columns.
I'll throw the kron solution out there. Just because.
kron(ones(4,5),tril(ones(3)))
More than 25 characters, but hey:
bsxfun(#le,mod(0:3*5-1,3),mod(0:3*4-1,3).')

Grouping logical data in Matlab

I have a matrix that contains data of 0 & 1. I want to find groups of ones (not a specific size) in that matrix. Is it possible somehow?
Thanks in advance!
If you mean that you want to find all the "connected components in the matrix, say BW, simply use:
BW = logical([1 1 1 0 0 0 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 0 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 1 1 0
1 1 1 0 0 0 0 0]);
L = bwlabel(BW,4) %Result
This would yeild:
L =
1 1 1 0 0 0 0 0
1 1 1 0 2 2 0 0
1 1 1 0 2 2 0 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 3 3 0
1 1 1 0 0 0 0 0
Now if you want to find the size of various groups:
for ii=1:max(L(:))
length_vector(ii)=length(find(L==ii));
end
length_vector
This gives you:
length_vector =
24 4 5