How to generalise my code?

2 views (last 30 days)
Hozifa
Hozifa on 30 Oct 2022
Answered: vidyesh on 28 Oct 2024
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 Comments
Torsten
Torsten on 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 on 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
];

Sign in to comment.

Answers (1)

vidyesh
vidyesh on 28 Oct 2024
Hello Hozifa,
You can use an array of cells for this purpose.
array2D = [
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
];
cellArray{1} = array2D;
array2D = [
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
];
cellArray{2} = array2D;
disp(cellArray);
{10x2 double} {8x2 double}
When processing the values add another for loop
for i = 1:2 % i will indicate group index
XX = cellArray{i}
xx=XX(:,1);
% Rest of Code
end
XX = 10×2
0.0000 -132.2080 0.0000 -149.3460 0.0000 -149.4710 0.0000 -153.9970 0.0000 -113.4620 0.0000 -122.7340 0.0000 -131.4050 0.0000 -134.7400 0.0000 -136.6120 0.0000 -137.4010
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
XX = 8×2
0.0000 -53.6080 0.0000 -63.8480 0.0000 -56.2850 0.0000 -69.4200 0.0000 -69.4460 0.0000 -67.7460 0.0000 -59.4430 0.0000 -68.5250
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Alternatively, you can keep track of each starting and ending index of each group.
In this secnario you can rewrite the code as:
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
];
G1Start = 1;
G1End = 10;
%%
% rest of code
xx = XX(G1Start:G1End,1);
Hope this helps.

Categories

Find more on Loops and Conditional Statements 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!