How to generalise my code?
Mostra commenti meno recenti
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
Risposte (1)
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);
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
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.
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!