The structure that I want to draw should be exactly like the one shown. Thanks!
Here's a little function I whipped up that will take as input a square, upper-triangular matrix and plot the lattice structure as above:
function hFigure = plot_lattice(A)
%# Compute all the coordinates needed for the lines and points:
N = size(A,1);
[xPoints,yPoints] = meshgrid(0:N-1);
yPoints = bsxfun(#plus,-yPoints,0:0.5:(N-0.5)/2);
xLines = [xPoints([1:N+1:N^2-N-1 1:N:N^2-2*N+1]); ...
xPoints([1:N-1 N:-1:2],N).']; %'
yLines = [yPoints([1:N+1:N^2-N-1 1:N:N^2-2*N+1]); ...
yPoints([1:N-1 N:-1:2],N).']; %'
index = find(triu(reshape(1:N^2,N,N)));
xPoints = xPoints(index);
yPoints = yPoints(index);
values = strtrim(cellstr(num2str(A(index))));
%# Create the figure:
hFigure = figure('Color','w');
hAxes = axes('Parent',hFigure,'XLim',[-0.5 N-0.5],...
'YLim',[min(yPoints)-0.5 max(yPoints)+0.5],...
'YColor','w','XTick',0:N-1,'LineWidth',2);
hold on;
plot(hAxes,xLines,yLines,'k','LineWidth',2);
plot(hAxes,xPoints,yPoints,'o','MarkerFaceColor',[0.96 0.96 0.86],...
'MarkerSize',30,'MarkerEdgeColor','k','LineWidth',2);
text(xPoints,yPoints,values,'Parent',hAxes,...
'HorizontalAlignment','center');
hold off;
end
And here's a test with a sample matrix:
>> A = triu(reshape(1:25,5,5))
A =
1 6 11 16 21
0 7 12 17 22
0 0 13 18 23
0 0 0 19 24
0 0 0 0 25
>> plot_lattice(A);
I have modified the code a little so it can print the nodes as well as multiple values in each node.
A is now a three dimension matrix and cant take also empty values (NaN) that are not printed in the tree
Ofc the code is not optimal... maybe one of you can improve it
%# Compute all the coordinates needed for the lines and points:
close all
[N,L] = size(A);
L=L/N;
[xPoints,yPoints] = meshgrid(0:N-1);
yPoints = bsxfun(#plus,-yPoints,0:0.5:(N-0.5)/2);
xLines = [xPoints([1:N+1:N^2-N-1 1:N:N^2-2*N+1]); xPoints([1:N-1 N:-1:2],N).'];
yLines = [yPoints([1:N+1:N^2-N-1 1:N:N^2-2*N+1]); yPoints([1:N-1 N:-1:2],N).'];
index = find(triu(reshape(1:N^2,N,N)));
xPoints = xPoints(index);
yPoints = yPoints(index);
% values = strtrim(cellstr(num2str(A(index))));
for i=1:L
values(:,i) = strtrim(cellstr(num2str(A((i-1)*N*N+index))));
end
values = strrep(values, 'NaN', ' ');
for i=1:N
for j=i:N
if i==1 && j==1
nodes(i,j)=cellstr(strcat('N_','0'));
else
nodes(i,j)=cellstr(strcat('N_','{',repmat('u',1,(j-1)-(i-1)),repmat('d',1,(i-1)),'}'));
end
end
end
nodes = nodes(index);
%# Create the figure:
hFigure = figure('Color','w');
hAxes = axes('Parent',hFigure,'XLim',[-0.5 N-0.5],'YLim',[min(yPoints)-0.5 max(yPoints)+0.5],'YColor','w','XTick',0:N-1,'LineWidth',2);
hold on;
plot(hAxes,xLines,yLines,'k','LineWidth',2);
plot(hAxes,xPoints,yPoints,'o','MarkerFaceColor',[0.96 0.96 0.86],'MarkerSize',60,'MarkerEdgeColor','k','LineWidth',2);
for i=1:L
text(xPoints,yPoints+L*0.05-(i-1)*0.1,values(:,i),'Parent',hAxes,'HorizontalAlignment','center');
end
text(xPoints-0.4,yPoints,nodes,'Parent',hAxes,'HorizontalAlignment','center');
hold off;
I would use matlab to generate a text file for use in Graphviz.
Related
I have the following code to generate a PRM map that will be used for A* application. There exist 2 problems with the code
It keeps the blue lines representing the original PRM where lines can cross over obstacles. I don't want to keep the blue lines but I couldn't find the way to remove them.
The green lines are going over obstacles even though they shouldn't
The code is as follows
clc;
clear all;
close all;
seed = 123512;
rng(seed);
xaxis = 100;
yaxis = 100;
obstacles = false(xaxis,yaxis);
[X,Y] = meshgrid(1:xaxis,1:yaxis);
obstacles(50:75,50:75) = true;
obstacles(25:35,30:40) = true;
obstacles(25:35,60:80) = true;
figure;
imshow(~obstacles,"InitialMagnification",1000);
axis([0 xaxis 0 yaxis]);
axis xy;
axis on;
%PRM PARAMETERS
max_nodes_connect = 4;
max_connect_len = 40;
segments = 1;
max_nodes_grid = 30;
skipped = 0;
%PRM ALGO
nodes = 0; %Counter
map = zeros(size(obstacles)); %generate map
map(obstacles) = 1; %put the obstacles
Graph_connections = Inf(max_nodes_grid,max_nodes_connect + 1); %rows = # nodes cols = ID and neighbors
while (nodes < max_nodes_grid)
node_x = randi(xaxis);
node_y = randi(yaxis);
if(map(node_y,node_x)==1 || map(node_y,node_x)==2)
continue;
end
nodes = nodes + 1; %a valid node generated
map(node_y,node_x) = 2; %2 means there exists a node at that location
hold on
scatter(node_x,node_y,"red","filled")
%NODES TO CONNECT
nodes_to_connect = [];
distances = [];
for i= 1:numel(Graph_connections(:,1))
if(Graph_connections(i,1)==Inf)
break
end
[row,col] = ind2sub(size(map),Graph_connections(i,1));
%Check if within range
if(norm([node_y,node_x]-[row,col])>max_connect_len)
continue;
end
line_on_obstacle = check_obstacle(map,node_x,node_y,row,col);
%Check if obstacle thru line HAS TO BE WRITTEN
if(line_on_obstacle)
disp("Check Obstacle: " + line_on_obstacle);
skipped = skipped + 1;
continue;
end
nodes_to_connect = [nodes_to_connect, Graph_connections(i,1)];
distances = [distances; [Graph_connections(i,1),norm([node_y,node_x]-[row,col])]];
end
Graph_connections(nodes,1) = sub2ind(size(map),node_y,node_x);
if(size(distances)>0)
sorted_distances = sortrows(distances,2);
for i = 1:min(max_nodes_connect,size(sorted_distances,1))
Graph_connections(nodes,i+1) = sorted_distances(i,1);
[row,col] = ind2sub(size(map),sorted_distances(i,1));
if(line_on_obstacle==false)
disp("Line is not on obstacle")
hold on
plot([node_x,col],[node_y,row],"green","LineWidth",1.5);
continue;
else
disp("Line is on obstacle: " + [node_x,col] + " " + [node_y,row]);
break;
end
end
disp("==========================")
end
end
function on_line = check_obstacle(map,node_x,node_y,row,col)
on_line = 0;
my_line = line([node_x,col],[node_y,row]);
line_spacing = max(abs(my_line.XData(1) - my_line.XData(2))+1,abs(my_line.XData(1) - my_line.XData(2))+1);
x_coordinates_line = round(linspace(my_line.XData(1),my_line.XData(2),line_spacing));
y_coordinates_line = round(linspace(my_line.YData(1),my_line.YData(2),line_spacing));
for i = 1:line_spacing
if(map(x_coordinates_line(i),y_coordinates_line(i))==1)
disp("ON OBSTACLE: " + x_coordinates_line(i) + " " + y_coordinates_line(i));
on_line = true;
break;
end
end
end
The check_obstacle function is used to check if the points on the line are in the boundaries of obstacles. What am I missing here?
close all;clear all;clc
format short
nx=100;ny=100; % grid size
[X,Y]=meshgrid(1:nx,1:ny);
Nobst=3 % amount obstacles
Nnet=30 % amount net points
Dmax=100 % net segment max length
% OBSTACLES
% define obstacles.
% Following are as defined in question
P1=[50 50; 75 50; 75 75; 50 75; 50 50];
P2=[25 30; 25 40; 35 40; 35 30; 25 30];
P3=[25 60; 25 80; 35 80;35 60;25 60];
% obstacle points all in one array
Pobst=[P1(:);P2(:);P3(:)];
Pobst=reshape(Pobst,[size(P1,1),size(P1,2),Nobst]);
% plot obstacles
hp=[]
figure(1)
ax=gca
hp=patch(squeeze(Pobst(:,1,:)),squeeze(Pobst(:,2,:)),[.5 .5 .5])
axis([1 nx 1 ny]);grid on
hp.EdgeAlpha=0;
ax.DataAspectRatio=[1 1 1]
hold(ax,'on')
% obstacle segments list : [x1 y1 x2 y2 d(X1,Y1)]
Lobst1=[]
for k=2:1:size(P1,1)
Lobst1=[Lobst1; [P1(k-1,:) P1(k,:) ((P1(k-1,1)-P1(k,1))^2+(P1(k-1,2)-P1(k,2))^2)^.5]];
end
Lobst2=[]
for k=2:1:size(P2,1)
Lobst2=[Lobst2; [P2(k-1,:) P2(k,:) ((P2(k-1,1)-P2(k,1))^2+(P2(k-1,2)-P2(k,2))^2)^.5]];
end
Lobst3=[]
for k=2:1:size(P3,1)
Lobst3=[Lobst3; [P3(k-1,:) P3(k,:) ((P3(k-1,1)-P3(k,1))^2+(P3(k-1,2)-P3(k,2))^2)^.5]];
end
Lobst=[Lobst1;Lobst2;Lobst3]
%% NETWORK
% all grid points outside obstacles
[in1,on1]=inpolygon(X(:),Y(:),P1(:,1),P1(:,2));
[in2,on2]=inpolygon(X(:),Y(:),P2(:,1),P2(:,2));
[in3,on3]=inpolygon(X(:),Y(:),P3(:,1),P3(:,2));
Xout=X(~in1 & ~in2 & ~in3);Yout=Y(~in1 & ~in2 & ~in3);
% plot(ax,Xout,Yout,'og','LineStyle','none') % check
% choose Nnet points outside obstacles
nP2=randi([1 numel(Xout)],Nnet,1);
Pnet=[Xout(nP2) Yout(nP2)];
plot(ax,Pnet(:,1),Pnet(:,2),'or','LineStyle','none')
% net segments list [x1 y1 x2 y2 d(X2,Y2) 0/1] 6th column [0 1] 1: draw 0: do not draw
nLnet=nchoosek([1:size(Pnet,1)],2);
Lnet=[Pnet(nLnet(:,1),1) Pnet(nLnet(:,1),2) ... % segment 1st point
Pnet(nLnet(:,2),1) Pnet(nLnet(:,2),2) ... % segment 2nd point
((Pnet(nLnet(:,1),1)-Pnet(nLnet(:,1),2)).^2+(Pnet(nLnet(:,2),1)+Pnet(nLnet(:,2),2)).^2).^.5 ... % segment length
ones(size(nLnet,1),1)];
% check all net links are plotted
for k=1:1:size(Lnet,1)
plot(ax,[Lnet(k,1) Lnet(k,3)],[Lnet(k,2) Lnet(k,4)],'b');
hold(ax,'on')
end
% remove segments longer than Dmax
Lnet=sortrows(Lnet,5,'descend');
[~,n1]=max(Lnet(:,5)<=Dmax);
Lnet(1:n1-1,:)=[];
for k=1:1:size(Lnet,1)
plot(ax,[Lnet(k,1) Lnet(k,3)],[Lnet(k,2) Lnet(k,4)],'r');
hold(ax,'on')
end
%%
Redrawing and NOT removing net segments longer than Dmax
close all
hp=[]
figure(1)
ax=gca
hp=patch(squeeze(Pobst(:,1,:)),squeeze(Pobst(:,2,:)),[.5 .5 .5])
axis([1 nx 1 ny]);grid on
hp.EdgeAlpha=0;
ax.DataAspectRatio=[1 1 1]
hold(ax,'on')
plot(ax,Pnet(:,1),Pnet(:,2),'or','LineStyle','none')
Lnet=[Pnet(nLnet(:,1),1) Pnet(nLnet(:,1),2) ... % segment 1st point
Pnet(nLnet(:,2),1) Pnet(nLnet(:,2),2) ... % segment 2nd point
((Pnet(nLnet(:,1),1)-Pnet(nLnet(:,1),2)).^2+(Pnet(nLnet(:,2),1)+Pnet(nLnet(:,2),2)).^2).^.5 ... % segment length
ones(size(nLnet,1),1)];
% check what pair segments intersect
for k2=1:1:size(Lnet,1)
allclear=ones(1,size(Lobst,1));
% allclear=zeros(1,size(Lobst,1));
for k1=1:1:size(Lobst,1)
% segments are contained in lines : check lines intersect
x1o=Lobst(k1,1);y1o=Lobst(k1,2);x2o=Lobst(k1,3);y2o=Lobst(k1,4);
x1n=Lnet(k2,1);y1n=Lnet(k2,2);x2n=Lnet(k2,3);y2n=Lnet(k2,4);
x1=x1o;x2=x2o;y1=y1o;y2=y2o;
x3=x1n;x4=x2n;y3=y1n;y4=y2n;
% t1 : x parameter
t1=((x1-x3)*(y3-y4)-(y1-y3)*(x3-x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
xp=x1+t1*(x2-x1); % xp : crossing x coordinage
% u1 : y parameter
u1=-((x1-x2)*(y1-y3)-(y1-y2)*(x1-x3))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
yp=y1+t1*(y2-y1); % yp : crossing y coordinate
% checks
plot(ax,x1o,y1o,'c*');plot(ax,x2o,y2o,'c*');plot(ax,[x1o x2o],[y1o y2o],'c')
plot(ax,x1n,y1n,'m*');plot(ax,x2n,y2n,'m*'); % plot(ax2,[x1n x2n],[y1n y2n],'m')
m1o=(y2o-y1o)/(x2o-x1o); % slopes
m2n=(y2n-y1n)/(x2n-x1n) ;
if (t1>=0 && t1<=1 && u1>=0 && u1<=1) && ...
(xp>=0 || xp<=nx || yp>=0 || yp<=ny)
allclear(k1)=0; % then do not plot this segment
end
end
if sum(allclear)==size(Lobst,1) %k2-th net segment hits no obstacles
plot(ax,x1n,y1n,'m*');plot(ax,x2n,y2n,'m*');plot(ax,[x1n x2n],[y1n y2n],'m')
elseif sum(allclear)~=size(Lobst,1)
Lnet(k2,end)=0;
end
end
Comments
1.- Note I have added format short because with format long there is decimal accretion right at the bottom of a few intersection points, that believe it or not, cause some false results.
2.- I like Torsen's explanation to find line segment intersections available here.
3.- There are faster ways to implement the main loop, but I make it go through all net-segment vs obstacle-element in case you may need it this way, to for instance count hits.
4.- There's also room for improvement in the way Lobst is generated.
5.- These lines
x1=x1o;x2=x2o;y1=y1o;y2=y2o;
x3=x1n;x4=x2n;y3=y1n;y4=y2n;
it's just an easy way to plug in formulation to already written lines, reducing amount variables is also left for next version.
Based on this idea (fig)
I am trying to plot all these three outputs (edges) in one single image with different colors to represent each, (not something like subplot, or imshowpair). Still now my outputs are just three different images.
clear all
close all
clc
%% ground truth
img = imread('22o.jpg');
img_gray = rgb2gray(img);
img_ground_truth = imread('22g.jpg');
img_ground_truth = im2bw(img_ground_truth);
cc = img_ground_truth;
%cc = img_ground_truth(11:54, 112:171);
%% Detected part
img_edge = edge(img_gray, 'canny');
dd = img_edge;
%dd = img_edge(11:54, 112:171);
%% True pixel, false pixel, true negative
[m n] = size(cc);
true_pixel = zeros(m,n);
false_pixel = zeros(m,n);
false_negative = zeros(m,n);
for i = 1:m
for j = 1:n
if (dd(i,j) == cc(i,j))
true_pixel(i,j) = dd(i,j);
elseif (dd(i,j)~= cc(i,j))
false_pixel(i,j) = dd(i,j);
end
end
end
for i = 1:m
for j = 1:n
if (cc(i,j)==1 && dd(i,j)==0)
false_negative(i,j) = cc(i,j);
end
end
end
% subplot(2,3,1); imshow(cc); title('Gt');
% subplot(2,3,2); imshow(dd); title('Dc');
figure(1),imshow(true_pixel, 'ColorMap',[1 1 1;0 1 0]);
title('True Pixel (TP)');
hold on
figure(2),imshow(false_pixel, 'ColorMap',[1 1 1;1 0 0]);
title('False Pixel (FP)');
hold on
figure(3),imshow(false_negative, 'ColorMap',[1 1 1;0 0 1]);
title('False Negative (FN)');
hold off
The original solution I suggested in the comments was:
merged_image = cat(3, false_pixel, true_pixel, false_negative);
imshow(merged_image)
This results in an image in which the True Negative pixels are black:
(Please forgive the legend at the top, I used your images and was too lazy to remove it.)
If there is any possibility of overlap between the three images, this is the approach I'd use. If you want TN to be white, instead of adding the pixels to the desired channel, you can subtract them from the other two channels:
% turn images into logical arrays to use in indexing
true_pixel = logical(true_pixel);
false_pixel = logical(false_pixel);
false_negative = logical(false_negative);
% create RGB channels for all-white image
r_channel = ones(size(true_pixel));
g_channel = ones(size(true_pixel));
b_channel = ones(size(true_pixel));
% leave pixels in true_pixel image green
r_channel(true_pixel) = 0;
b_channel(true_pixel) = 0;
% leave pixels in false_pixel image red
g_channel(false_pixel) = 0;
b_channel(false_pixel) = 0;
% leave pixels in false_negative image blue
r_channel(false_negative) = 0;
g_channel(false_negative) = 0;
% merge into RGB image
merged_image = cat(3, r_channel, g_channel, b_channel);
imshow(merged_image)
Result:
Another possibility is to use indexed images as you did originally. The cleanest way to do that is to generate the different indices {1,2,3} in a merged image within your loop, so something like this in your last loop (and similar code in the other two):
...
false_negative(i,j) = cc(i,j);
merged_image(i,j) = cc(i,j)*3;
...
Then at the end, combine all 3 colors into one colormap:
imshow(uint8(merged_image), 'ColorMap', [1 1 1; 0 1 0; 1 0 0; 0 0 1])
I need to plot horizontal bars using the grouped style such that all the bars belonging to each group have the same color but different from other groups (i.e. all bars of the top group are red, the group below it - green, and so on...).
Also, how can I put the values on the top of each bar horizontally? How can I control the position of such values?
This is my code:
y = [91.9 8.1 94.4 5.6; 84.9 15.1 90.12 9.88; 89.4 10.6 91.2 8.8; 72 28 50.9 49.1];
h = barh(y,'grouped');
job = {'group1','group2 ','group 3','group4'};
legend(job,'location','northeast');
This is my current figure:
Here's a hack:
Plot bar graph for 1 row at a time and fill the space with NaNs while specifying a single color for all rows. Plotting NaNs will plot nothing.
[ry, cy] = size(y); %Number of rows and columns of y
hold on;
for k = 1:ry
tmp = [NaN(k-1,cy); y(k,:); NaN(4-k,cy)]; %Filling with NaNs
h{k} = barh(tmp, 'FaceColor', rand(1,3)); %Plotting bar graph in random color
h{k} = h{k}(1); %Store handle of any of the rows (Required for legend)
end
job = {'group1', 'group2', 'group3', 'group4'}; %Required legend entries
legend([h{:}], job); %Default location is already 'northeast' (so can be skipped)
Output:
This is another type of hack; tested on R2017b.
The reason why what you're asking is difficult to do is because each group in a bar plot is actually a Quadrilateral object (i.e. polygon). For example, let's take the 1st Bar object:
>> h(1).Face.VertexData
ans =
3×16 single matrix
Columns 1 through 9
0 91.9000 91.9000 0 0 84.9000 84.9000 0 0
0.6545 0.6545 0.8000 0.8000 1.6545 1.6545 1.8000 1.8000 2.6545
0 0 0 0 0 0 0 0 0
Columns 10 through 16
89.4000 89.4000 0 0 72.0000 72.0000 0
2.6545 2.8000 2.8000 3.6545 3.6545 3.8000 3.8000
0 0 0 0 0 0 0
And if we add this line to the code:
drawnow; hold on; scatter(h(1).Face.VertexData(1,:), h(1).Face.VertexData(2,:));
we get (notice the green circles):
What's interesting is that the points in VertexData are not read-only, which means we can just intelligently "rearrange" the VertexData matrices of the N bar groups to get the desired result. Here's one way to do it (notice I renamed h to hBar):
% Obtain the old VertexData:
vd_old = cell2mat(reshape(get([hBar.Face],'VertexData'),1,1,[]));
% Rearrange VertexData:
nB = numel(hBar);
vd_new = cell2mat(permute(mat2cell(vd_old,3,repelem(4,nB),repelem(1,nB)),[1,3,2]));
% Assign the new VertexData back into the Bar objects' Edge and Face fields:
for indB = 1:nB
hBar(indB).Edge.VertexData = vd_new(:,:,indB);
hBar(indB).Face.VertexData = vd_new(:,:,indB);
end
Finally, we manually add labels using the text command:
xOffset = 1;
for indB = 1:nB
text(double(vd_new(1,2:4:end,indB)) + xOffset, double(tmpY(2:4:end)), ...
string(vd_new(1,2:4:end,indB)),'VerticalAlignment','middle');
end
The end result:
The final code:
function q47978293
close all force;
y = [91.9 8.1 94.4 5.6; 84.9 15.1 90.12 9.88; 89.4 10.6 91.2 8.8; 72 28 50.9 49.1];
figure(47978293); set(gcf,'Position',[786,556,887,420]); hBar = barh(y,'grouped');
legend("group" + (1:4),'location','northeast');
drawnow;
% hold on; scatter(h(1).Face.VertexData(1,:), h(1).Face.VertexData(2,:));
% Obtain the old VertexData:
vd_old = cell2mat(reshape(get([hBar.Face],'VertexData'),1,1,[]));
% Rearrange VertexData:
nB = numel(hBar);
vd_new = cell2mat(permute(mat2cell(vd_old,3,repelem(4,nB),repelem(1,nB)),[1,3,2]));
% Assign the new VertexData back into the Bar objects' Edge and Face fields:
xOffset = 1; % < Adjust as you see fit
for indB = 1:nB
hBar(indB).Edge.VertexData = vd_new(:,:,indB);
hBar(indB).Face.VertexData = vd_new(:,:,indB);
try
text( y(indB,:) + xOffset, mean(reshape(vd_new(2,1:2:end,indB),2,[]),1,'double'), ...
string(vd_new(1,2:4:end,indB)),'VerticalAlignment','middle');
catch % Compatibility fix for R2016b & R2017a. Credit #SardarUsama
text( y(indB,:) + xOffset, mean(reshape(vd_new(2,1:2:end,indB),2,[]),1,'double'), ...
split(num2str(vd_new(1,2:4:end,indB))),'VerticalAlignment','middle');
end
end
end
Note: due to the hacky nature of this solution, if the figure is redrawn (due to e.g. resizing), the bar colors will return to their original state - so make sure that the part that changes colors is the very last thing you do.
P.S.
The R2017b release notes state that this customization should be officially supported now (through the CData property of Bar objects), but I couldn't get the legend to display the correct colors. If you want to try it, just plot using the following command (R2017b+):
barh(y,'grouped','FaceColor','flat','CData',lines(size(y,1)));
The two requests overlap making everything kinda complex, since using uniform colors for each group implies using a workaround that needs another workaround to male bar text work. Here is the code:
y = [
91.90 8.10 94.40 5.60;
84.90 15.10 90.12 9.88;
89.40 10.60 91.20 8.80;
72.00 28.00 50.90 49.10
];
[rows,cols] = size(y);
n = NaN(rows,cols);
c = {'r' 'g' 'b' 'y'};
bh = cell(rows,1);
lh = cell(rows,1);
hold on;
for k = 1:rows
curr = n;
curr(k,:) = y(k,:);
bar = barh(curr,'FaceColor',c{k});
bh{k} = bar;
lh{k} = bar(1);
end
hold off;
legend([lh{:}],{'G1','G2','G3','G4'},'Location','southoutside');
yl = get(gca,'XLim');
yl_up = yl(2);
xoff = yl_up * 0.01;
set(gca,'XLim',[yl(1) (yl_up + (yl_up * 0.1))]);
set(gcf,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
for i = 1:numel(bh)
bh_i = bh{i};
for j = 1:numel(bh_i)
bh_ij = bh_i(j);
hx = bh_ij.YData + xoff;
hy = bh_ij.XData + bh_ij.XOffset;
text(hx,hy,num2str(hx.','%.2f'), ...
'FontSize',10, ...
'HorizontalAlignment','left', ...
'VerticalAlignment','middle');
end
end
And here is the output:
I have the following code:
limits = [-1 1 -1 1];
g1 = [1;2;3;4;5;6;7;8];
col = [1 1 0; 1 0 1; 0 1 1; 1 0 0; 0 1 0; 0 0 1; 0 0 0; 0.5 0.3 0.1];
sym = 'vvvv^^^^';
sym2 = 'xxxxxxxx';
points = 30;
for i = 1:8;
mhuR(i,1) = mean(HSRXdistpR(i,:));
mhuR(i,2) = mean(HSRYdistpR(i,:));
mhuL(i,1) = mean(HSRXdistpL(i,:));
mhuL(i,2) = mean(HSRYdistpL(i,:));
mhuX(i,1) = mean(TotxcomHSRpX(i,:));
mhuX(i,2) = mean(TotxcomHSRpY(i,:));
CR{i} = cov(HSRXdistpR(i,:),HSRYdistpR(i,:));
CL{i} = cov(HSRXdistpL(i,:),HSRYdistpL(i,:));
CX{i} = cov(TocomXdistp(i,:),TocomYdistp(i,:));
ellipR{i} = uncertEllip(CR{i},mhuR(i,:),points);
ellipL{i} = uncertEllip(CL{i},mhuL(i,:),points);
ellipX{i} = uncertEllip(CX{i},mhuX(i,:),points);
end
figure; hold on
scatter(HSRXdistbR2,HSRYdistbR2,'ko'); hold on
scatter(HSRXdistbL2,HSRYdistbL2,'ko'); hold on
scatter(TocomXdistb2,TocomYdistb2,'kx'); hold on
gscatter(HSRXp2(:,1),HSRYp2(:,1),g1,col,sym), hold on
gscatter(HSRXp2(:,2),HSRYp2(:,2),g1,col,sym), hold on
gscatter(copHSRXp2(:,2),copHSRYp2(:,2),g1,col,sym2), hold on
for i = 1:8;
plot(ellipR{i}(:,1),ellipR{i}(:,2),col(i,:)), hold on
plot(ellipL{i}(:,1),ellipL{i}(:,2),col(i,:)), hold on
plot(ellipX{i}(:,1),ellipX{i}(:,2),col(i,:)), hold on
end
vline(0)
hline(0)
legend('base','base','base','-0.04m', '-0.08m', '-0.12m', '-0.16m', '0.04m', '0.08m', '0.12m', '0.16m');
title({'xCoM and left and right foot placement relative',...
'to xCoM per perturbation, for all subjects'})
axis(limits)
xlabel('x displacement (scaled)');
ylabel('y displacement (scaled)');`
I'm trying to get the line plots (second for loop) to have the same colors as the scatter plot data (the uncertainty ellipses each belong to their own scatter point). However, it won't allow me to use the colors from the col matrix. Am I missing something here?
Error:
Data must be a single matrix Y or a list of pairs X,Y.
Not trying to define the colors works fine, but of course the colors don't match.
Try replacing each of those erroring lines with this:
plot(ellipR{i}(:,1),ellipR{i}(:,2),'Color',col(i,:));
I have a multispectral image with 6 bands.
imagen1=imread('re_2008.tif')
size2=size(imagen1);
nrow= size2(1);
ncol= size2(2);
nband= size2(3);
What I want to do is get every pixel of every band (in the same position) with their value, do an interpolation and with a new value located in other wavelength, replace it. Maybe you understand me more if I show you the code.
imagen3_2 = zeros(nrow, ncol, nband);
var1= [1 2 3 4 5 6]'; %'
for row=1:nrow;
for column=1:ncol;
for band=1:nband;
v = imagen1(nrow(1),nband(2),:); v = v(:);
t= 0:100;
interplan= interp1(var1, v, t,'cubic');
y5 = interplan(5); % I get the value of the interpolation on this position
y12 = interplan(12);
y20 = interplan(20);
y50 = interplan(50);
y80 = interplan(80);
y90 = interplan(90);
imagen3_2(:,:,1)= (y5); % and I replace it
imagen3_2(:,:,2)= (y12);
imagen3_2(:,:,3)= (y20);
imagen3_2(:,:,4)= (y50);
imagen3_2(:,:,5)= (y80);
imagen3_2(:,:,6)= (y90);
end
end
end
I get the same value as a result, not for every pixel.
Thanks in advance,
How about
imagen3_2 = zeros(nrow, ncol, nband);
var1= [1 2 3 4 5 6]'; %'
for row=1:nrow;
for column=1:ncol;
v = imagen1(row, column, :); v = v(:);
t= 0:100;
interplan= interp1(var1, v, t,'cubic');
y5 = interplan(5); % I get the value of the interpolation on this position
y12 = interplan(12);
y20 = interplan(20);
y50 = interplan(50);
y80 = interplan(80);
y90 = interplan(90);
imagen3_2(row,column,1)= (y5); % and I replace it
imagen3_2(row,column,2)= (y12);
imagen3_2(row,column,3)= (y20);
imagen3_2(row,column,4)= (y50);
imagen3_2(row,column,5)= (y80);
imagen3_2(row,column,6)= (y90);
end
end
I see that Shai already answered your question, however if you are not really interested in variables y5, y12 etc. there is a way to make the code compacter and easier to maintain:
imagen3_2 = zeros(nrow, ncol, nband);
var1= [1 2 3 4 5 6]'; %'
for row=1:nrow;
for column=1:ncol;
v = imagen1(row, column, :); v = v(:);
t= 0:100;
interplan= interp1(var1, v, t,'cubic');
y = [5 12 20 50 80 90];
for i = 1:length(y)
imagen3_2(row,column, i)= interplan(y(i));% repace interpolation value
end
end
end
For efficiency, try to avoid loops in Matlab, you can try something like this:
x = [1 2 3 4 5 6];
y = imread('re_2008.tif');
y_size = size((y(:,:,1)));
xi = 0:100;
yi = zeros([y_size,numel(xi)]);
for i_pix=1:prod(y_size)
[aux_x,aux_y] = ind2sub(y_size,i_pix);
yi(aux_x,aux_y,:) = interp1(x,squeeze(y(aux_x,aux_y,:)),xi,'cubic');
end
Or even like this:
x = [1 2 3 4 5 6];
y = imread('re_2008.tif');
xi = 0:100;
yi = arrayfun(#(y1,y2,y3,y4,y5,y6) interp1(x,[y1,y2,y3,y4,y5,y6],xi,'cubic'), ...
y(:,:,1), y(:,:,2), y(:,:,3), y(:,:,4), y(:,:,5), y(:,:,6))
I couldn't check the code, so there could be errors; but it is just to give ideas about other kinds of implementation.