# Having trouble with invalid frequency value of the colourmap for Scatter plot

2 views (last 30 days)
IMAN SYAHMI BIN JASNI on 19 Dec 2021
Answered: Image Analyst on 19 Dec 2021
Hello there!
I'm working on a monte carlo simulation for particle scattering and having trouble with the colourmap representing frequency of the y variable on my scatter plot, Final energy of particle. I hope you can help me out and explain what went wrong.
It gives me this error message:
==========================
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Scatteringsimulation (line 103)
cMatrix=colors(afhist,:);
========================== clc
clear
clearvars
tic
%Constants%
%=========%
h=6.6261e-34;
c=2.9979e8;
c2=c^2;
m0= 9.10956e-31;
%Variables%
%=========%
A=15.000e3;
n=1e7;
m=2*n;
D=224.060e3;
Energydist_Beta= normrnd(D,10000,[1,n]);
Energydist_Anti= normrnd(D,10000,[1,m]);
%G=gpuArray(Energydist);
Scb=zeros(1,n);
Efb=zeros(1,n);
Sca=zeros(1,2*n);
Efa=zeros(1,2*n);
Freq_Eb=zeros(1,A);
Freq_Ea=zeros(1,A);
%Compton scattering of the electron using Langrangian method%
%===========================================================%
parfor i=1:n
B=Energydist_Beta(i);
s=0;
while B >= A
C = ((randi([0 200])-100)/100)*B*(cos((((randi([0 180]))-90)/180)*pi));
if C>A
s = s + 1;
end
B = B - C;
end
Scb(i)= s;
Efb(i)= floor(B);
end
parfor j=1:m
B1=Energydist_Anti(j);
s1=0;
while B1 >= A
C1 = ((randi([0 200])-100)/100)*B1*(cos((((randi([0 180]))-90)/180)*pi));
if C1>A
s1 = s1 + 1;
end
B1 = B1 - C1;
end
Sca(j)=floor(s1);
Efa(j)=floor(B1);
end
for k=1:A
Freq_Eb(k)= sum(Efb==k);
Freq_Ea(k)= sum(Efa==k);
end
afhist=hist(Efa,A);
mx= max(afhist);
colors= jet(mx);
colormap(colors);
cMatrix=colors(afhist,:);
[ra,ca]=size(Freq_Ea);
[rb,cb]=size(Freq_Eb);
MaxScattered_Beta=floor(Energydist_Beta/A);
MinEnergy_Beta=(Energydist_Beta-(A*MaxScattered_Beta));
MaxScattered_Anti=floor(Energydist_Anti/A);
MinEnergy_Anti=(Energydist_Anti-(A*MaxScattered_Anti));
Scattered_Beta = sum(Scb);
Scattering_efficiency_Beta = Scattered_Beta / n;
Scattered_Anti = sum(Sca);
Scattering_efficiency_Anti = Scattered_Anti / n;
aSc=reshape(Sca,m,1);
bSc=reshape(Scb,n,1);
aEf=reshape(Freq_Ea,ca,ra);
bEf=reshape(Freq_Eb,cb,rb);
pd_anti=fitdist(aSc,'poisson');
pd_beta=fitdist(bSc,'poisson');
Results=[Scattered_Beta,Scattered_Anti;Scattering_efficiency_Beta,Scattering_efficiency_Anti];
Performance_AntivsBeta=Scattered_Anti/Scattered_Beta;
disp(Results)
disp(pd_beta)
disp(pd_anti)
disp(Performance_AntivsBeta)
LimS=max(Sca);
Limt=max(Scb);
LimF=0.25*n;
tiledlayout(2,4)
ax1=nexttile;
histfit(Scb,Limt,'poisson')
set(gca,'XLim', [0 LimS]);
set(gca,'YLim', [0 LimF]);
xlabel('Number of Maximum Scatters Achieved')
ylabel('Frequency')
title('Distribution of particles scattered by Beta Minus particles' )
ax2=nexttile;
histfit(Sca,LimS,'poisson')
set(gca,'XLim', [0 LimS]);
set(gca,'YLim', [0 LimF]);
xlabel('Number of Maximum Scatters Achieved')
ylabel('Frequency')
title('Distribution of particles scattered by Beta Plus particles' )
ax3=nexttile;
histfit(MaxScattered_Beta,50,'poisson')
xlabel('Maximum Number of scatter expected')
ylabel('Frequency')
title('Distribution of mean particle scattered by Beta Minus')
ax4=nexttile;
histfit(MaxScattered_Anti,50,'poisson')
xlabel('Maximum Number of scatter expected')
ylabel('Frequency')
title('Distribution of mean particle scattered by Beta Plus')
ax5=nexttile;
scatter(Scb,Efb,1,cMatrix,'filled')
colorbar
colormap(colors);
set(gca,'XLim', [0 LimS]);
ylabel('Final Energy of electron (eV)')
xlabel('Number of Maximum Scatters Achieved')
ax6=nexttile;
scatter(Sca,Efa,1,cMatrix,'filled')
colorbar
colormap(colors);
set(gca,'Xlim', [0 LimS]);
ylabel('Final Energy of electron (eV)')
xlabel('Number of Maximum Scatters Achieved')
ax7=nexttile;
scatter(MaxScattered_Beta,MinEnergy_Beta,".")
ylabel('Final Energy of electron (eV)')
xlabel('Maximum Number of scatter expected')
ax8=nexttile;
scatter(MaxScattered_Anti,MinEnergy_Anti,".")
ylabel('Final Energy of electron (eV)')
xlabel('Maximum Number of scatter expected')
toc

Voss on 19 Dec 2021
afhist has at least one element which is zero. Zero is not a valid index in MATLAB, so you cannot use afhist as an index as it is now. This is the reason for the error.
IMAN SYAHMI BIN JASNI on 19 Dec 2021
I changed afhist=hist(Efa,A) to afhist=hist(Efa,unique(Efa)) and got this error instead Image Analyst on 19 Dec 2021
Try
cMatrix = colors(afhist + 1, :);
IMAN SYAHMI BIN JASNI on 19 Dec 2021
I got this error instead.so for color i chaged it to color=jet(mx+1) and still gives error for color. Image Analyst on 19 Dec 2021
I had to change parfor to for since it hung when I tried parfor.
This doesn't make sense:
afhist=hist(Efa,A);
mx= max(afhist);
colors= jet(mx);
colormap(colors);
cMatrix=colors(afhist,:);
First of all A is 15000. Why do you want a histogram with 15 thousand bins?
Second, even if you do that, you then find the max bin count, which is 14,760, and then you try to make a colormap with that many colors in it. Normally you won't have more than 256 colors in it. Why do you want so many colors?
Third, you don't even plot the histogram so what is the colormap supposed to be applied to? You're not displaying any bar chart (most common for histograms), plot, or image. So when you do colormap(colors) what do you expect to see since nothing is displayed.
It seems like you're trying to create a marker for every color where the color of the marker depends on the frequency of occurrence for that data point. Is that right? If so I think you'll need to get different colors for each data set Efa and Efb since they have different histograms and different numbers of points.

R2021b

### Community Treasure Hunt

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

Start Hunting!