overlap of a scatter plot on a 2D imagesc matrix

3 views (last 30 days)
Hi,
first of all I apologize if this might be an 'easy' question to answer or something has been discussed already.
I have created a 2D matrix and imaged it with imagesc function, as each pixel of this matrix represents a certain value.
I have to plot or scatter the locus of points corresponding to the middle line encompassed in the nonzero area of this matrix and I have some issues in visualizing it correctly.
I have created x_half_vec and y_half_vec which contain the x coordinate in pixels of the 2D map at half of the non-zero range along the x axis and for each row of the matrix, and its correspective value , respectively.
How can I overlap this line or locus of points on top of this image? I am nor sure x_half_vec and y_half_vec are even necessary
Thank you to anybody will answer me!
Here after the code:
clear all, close all, clc
n0= 1; % Refractive index of material 1
n1= 1.51; % Refractive index of material 2 (n1>n0)
alpha_in=[-30:0.5:65]; % Source incident angle [deg]
x=size(alpha_in);
x=x(2);
beta=[55:0.1:80]; % Wedge angle
y=size(beta);
y=y(2);
eta=zeros(y,x);
eta_half=zeros(y,x);
x_half_vec=zeros(1,x);
y_half_vec=zeros(1,y);
% return
n=n1/n0;
alpha_refr=asind((n0/n1).*sind(alpha_in)); % Refracted angle [deg]
r_s=(cosd(alpha_in)-n.*cosd(alpha_refr))./(cosd(alpha_in)+n.*cosd(alpha_refr));
r_p=(n.*cosd(alpha_in)-cosd(alpha_refr))./(n.*cosd(alpha_in)+cosd(alpha_refr));
t_s=1+r_s;
t_p=1/n.*(1+r_p);
R_s=abs(r_s).^2
R_p=abs(r_p).^2
T_s=(n1.*cosd(alpha_refr))./(n0.*cosd(alpha_in)).* abs(t_s).^2
T_p=(n1.*cosd(alpha_refr))./(n0.*cosd(alpha_in)).* abs(t_p).^2
R_eff=0.5*(R_s+R_p);
% return
for i=1:1:y
for j =1:1:x
if (alpha_in(j) <= asind(n1*sind(beta(i)-42))) && (alpha_in(j)>= asind(n1*sind(2*beta(i)-138)))
eta(i,j)=1-R_eff(j);
% disp('True')
[row,col]= find(eta(i,:)); % Non-zero pixel values index
x_half=round((col(1)+col(end))/2); % x index in the middle of the range in pixels
y_half=eta(i,x_half); % correspective y value
x_half_vec(1,i)=x_half; % vector of middle points on x axis in pixels
y_half_vec(1,j)=y_half;
else
eta(i,j)=0;
% disp('Not satisfied')
end
end
end
figure; imagesc(alpha_in,beta,eta); colorbar;
set(gca,'YDir','normal')
set(gca,'FontSize',16)
caxis ([0.85 1])
xlabel('Incident angle \alpha [deg])','FontSize',18)
ylabel('Wedge angle \beta [deg]','FontSize',18)
title ('Coupling efficiency [%]')

Accepted Answer

Mathieu NOE
Mathieu NOE on 14 Feb 2022
hello Giulia
maybe this ?
clear all, close all, clc
n0= 1; % Refractive index of material 1
n1= 1.51; % Refractive index of material 2 (n1>n0)
alpha_in=[-30:0.5:65]; % Source incident angle [deg]
x=size(alpha_in);
x=x(2);
beta=[55:0.1:80]; % Wedge angle
y=size(beta);
y=y(2);
eta=zeros(y,x);
eta_half=zeros(y,x);
x_half_vec=zeros(1,x);
y_half_vec=zeros(1,y);
% return
n=n1/n0;
alpha_refr=asind((n0/n1).*sind(alpha_in)); % Refracted angle [deg]
r_s=(cosd(alpha_in)-n.*cosd(alpha_refr))./(cosd(alpha_in)+n.*cosd(alpha_refr));
r_p=(n.*cosd(alpha_in)-cosd(alpha_refr))./(n.*cosd(alpha_in)+cosd(alpha_refr));
t_s=1+r_s;
t_p=1/n.*(1+r_p);
R_s=abs(r_s).^2;
R_p=abs(r_p).^2;
T_s=(n1.*cosd(alpha_refr))./(n0.*cosd(alpha_in)).* abs(t_s).^2;
T_p=(n1.*cosd(alpha_refr))./(n0.*cosd(alpha_in)).* abs(t_p).^2;
R_eff=0.5*(R_s+R_p);
% return
for i=1:1:y
for j =1:1:x
if (alpha_in(j) <= asind(n1*sind(beta(i)-42))) && (alpha_in(j)>= asind(n1*sind(2*beta(i)-138)))
eta(i,j)=1-R_eff(j);
% disp('True')
[row,col]= find(eta(i,:)>eps); % Non-zero pixel values index
x_half=round((col(1)+col(end))/2); % x index in the middle of the range in pixels
y_half=eta(i,x_half); % correspective y value
x_half_vec(1,i)=x_half; % vector of middle points on x axis in pixels
y_half_vec(1,j)=y_half;
else
eta(i,j)=0;
% disp('Not satisfied')
end
end
end
figure; hold on
imagesc(alpha_in,beta,eta); colorbar;
set(gca,'YDir','normal')
set(gca,'FontSize',16)
caxis ([0.85 1])
xlabel('Incident angle \alpha [deg])','FontSize',18)
ylabel('Wedge angle \beta [deg]','FontSize',18)
title ('Coupling efficiency [%]')
plot(alpha_in(x_half_vec),beta,'r','linewidth',8);
hold off
  9 Comments
Mathieu NOE
Mathieu NOE on 2 Mar 2022
hello Giulia
thanks for the update - glad you have a solution that works
Good luck for the future !

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!