How to generalise my code?

1 vue (au cours des 30 derniers jours)
Hozifa
Hozifa le 30 Oct 2022
Commenté : Hozifa le 30 Oct 2022
Hello everybody,
I have written a code, it works fine as long as I am taking each group of XX alone, I need to find away to run the whole code without the need to make some lines commented.
[Each group represents rays coming at a specific location, so I can't make XX to be treated as a one matrix, also please note that usually the number of groups is in the range of 10-25, that is why I want to generalize the code]
Thanks in advance
% This file estimates number of clusters and number of rays within each
% cluster
clear
clc
XX=[
4.23E-08 -132.208
4.60E-08 -149.346
4.60E-08 -149.471
5.00E-08 -153.997
5.62E-08 -113.462
6.88E-08 -122.734
7.98E-08 -131.405
7.98E-08 -134.74
7.98E-08 -136.612
1.00E-07 -137.401
% 1.50E-08 -53.608
% 1.92E-08 -63.848
% 2.01E-08 -56.285
% 2.18E-08 -69.42
% 2.18E-08 -69.446
% 2.20E-08 -67.746
% 2.88E-08 -59.443
% 2.88E-08 -68.525
% 1.67E-08 -54.499
% 1.84E-08 -43.949
% 1.84E-08 -73.698
% 2.03E-08 -63.327
];
xx=XX(:,1); % We take the times in the matrix
X=1; % the first cluster
x=1; % The number of ray in the first cluster is 1.
k=1; % startup value (the number belongs to the cluster number)
u(1,:)=[1 1]; % startup value
for y=2:length(xx)
b=xx(y)-xx(y-1); % find difference between two consecutive values starting from ray 2.
if b<=6.0e-9 % if difference is less or equal 6 ns
x=x+1; % The number of rays within the same cluster is two
u(k,:)=[X x];
else
X=X+1; % the number of cluster is added by 1.
x=1; % the number of rays in the new cluster is 1.
k=k+1;
u(k,:)=[X x];
end
end
clear b
[aa aaa]=size(u);
w=u(aa,1) % Gives the number of clusters
cls=[u(:,2)] % Gives the number of rays per cluster
  2 commentaires
Torsten
Torsten le 30 Oct 2022
The code also runs without errors after uncommenting the lines in XX.
So you will have to explain what you want to do and what you don't like in the result you get.
Hozifa
Hozifa le 30 Oct 2022
Thank you Torsten,
Yes the code will work but the results will be wrong, if you consider XX matrix, I want to run the code for each group of XX alone. the reason why I want to make them seperate, as each group considers a specific location where I recieved rays with different delays, so group 1 represents rays received at location A, group 2 represents rays at location B and so on. Thanks alot for your help
XX=[
4.23E-08 -132.208 % Group 1
4.60E-08 -149.346
4.60E-08 -149.471
5.00E-08 -153.997
5.62E-08 -113.462
6.88E-08 -122.734
7.98E-08 -131.405
7.98E-08 -134.74
7.98E-08 -136.612
1.00E-07 -137.401
1.50E-08 -53.608 % Group 2
1.92E-08 -63.848
2.01E-08 -56.285
2.18E-08 -69.42
2.18E-08 -69.446
2.20E-08 -67.746
2.88E-08 -59.443
2.88E-08 -68.525
1.67E-08 -54.499 % Group 3
1.84E-08 -43.949
1.84E-08 -73.698
2.03E-08 -63.327
];

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by