Matlab code modification request

1 vue (au cours des 30 derniers jours)
Hozifa
Hozifa le 25 Oct 2022
Commenté : Alex Hanes le 26 Oct 2022
Hi there,
I have this code, the code takes 10 rows at a time from the XX matrix, and then does the work, then it takes the next 10 rows and repeats the job (i.e. it will take 1-10 rows, then 11-20 rows,... etc). I have a problem, the number of rows is not necessarily to be always 10 (i.e. 9, 7 or even 2) for example the first group is 10 rows, the next group is 8, the next group is 3, the next group is 10, ....etc, how can I let the code to consider each group separately regardless to how many rows it has?
Remarks: 1. The second for loop is the core of the code.
2. Usually XX has 40-50 groups (we have 3 groups in this example for simplicity)
Thanks in advance
% This file estimates number of clusters and number of rays within each
% cluster
clear
clc
XX=[
2.00E-08 -47.401
4.47E-08 -58.497
4.89E-08 -62.685
4.47E-08 -64.481
3.33E-08 -74.794
6.91E-08 -75.094
3.64E-08 -83.07
3.64E-08 -83.398
1.03E-07 -133.076
1.18E-07 -154.731
2.15E-08 -52.17
4.54E-08 -55.037
4.54E-08 -58.191
4.72E-08 -62.973
6.99E-08 -84.227
5.63E-08 -105.681
3.70E-08 -133.107
1.02E-07 -133.134
1.17E-07 -138.002
3.98E-08 -141.03
4.62E-08 -55.239
2.31E-08 -55.546
4.62E-08 -58.321
4.56E-08 -63.548
5.79E-08 -100.341
9.99E-08 -133.124
3.79E-08 -134.177
4.06E-08 -141.204
4.44E-08 -141.623
4.56E-08 -148.638
];
xx=XX(:,1);
c=length(xx);
r=c/10; % this define how many rotations we will need
rr=1;
rrr=10;
cls=[];
for s=1:r
A=xx(rr:rrr,:); % We take each time 10 TOA
rrr=rrr+10; % Increase upper limit by 10
rr=rr+10; % increase lower limit by 10
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A=A*1.0e9;
B = sort(A,'ascend');
X=1;
x=1;
k=1;
u(1,:)=[1 1];
for y=2:length(B)
b=B(y)-B(y-1);
if b<=6
x=x+1;
u(k,:)=[X x];
else
X=X+1;
x=1;
k=k+1;
u(k,:)=[X x];
end
u;
end
u;
[aa aaa]=size(u);
ww=u(aa,1);
cls=[cls;u(:,2)];
w(s)=[ww];
clear X x k l B ww aa aaa u b
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
w' % Gives you the number of clusters
cls ; % Gives the number of rays per cluster
  1 commentaire
Alex Hanes
Alex Hanes le 26 Oct 2022
From the looks of it, the whole code is dependent upon sorting things by groups of 10. It's hard to follow the full code without knowing what it's purpose is or how things are sorted based on the "if b<=6" condition. Here are some tips to improve this code, then you can think about how to modify it for variable sizes.
  1. Define "10" one time, then call that variable wherever you call "10" elsewhere in the code. If 10 is the number of rays, then you might write: nRays = 10.
  2. Line 43 (r = c/10) requires that dimension 1 of your array is an integer multiple of 10.
  3. Lines 64 and 69 are the same. Both quantities (X, x) are updated in the if-else statement, so you can move this outside of if-else statement.
  4. The u; lines do no do anything.
  5. You can probably vectorize the inner loop for y=2:length(B) by using b = diff(B).
  6. Look at all of the squiggly lines and tips that MATLAB gives you when the code is open in the Editor and follow the tips to improve your code.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by