필터 지우기
필터 지우기

How to generalise my code?

조회 수: 1 (최근 30일)
Hozifa
Hozifa 2022년 10월 30일
댓글: Hozifa 2022년 10월 30일
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
Torsten
Torsten 2022년 10월 30일
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 2022년 10월 30일
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
];

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by