Hello
I am confused in writing a code which i am going to explain. imagine there is a row matrix whose dimension is not clear (as in each iteration there may be more arrays in this row vector), e.g. ccc=[10 20 30 40 50 ...]. On the other hand, there is a matrix (called Length(i)) that measures the length of some segments along a curve with respect to an origin. There is a length critteria called:J.
if Length(i)<J then assign ccc1 to that segment
if Length(i)>J or Length(i)<2*J then assign ccc2 to the second segment
if Length(i)>2*J or Length(i)<3*J then assign ccc3 to the third segment
.
.
.
It is worthy to point that Length(i)<J represents segment 1, J<Length(i)<2*J represents segment 2, etc..
it is not clear how many segments and c matrix arrays we would have.. Any recommendation/idea is highly appreciated.
A draft of my code is attached below:
% for i=1:n % n is the number of segments
% Length(i)=sqrt(abs((H(i,1)-PP(1,1))^2+((H(i,3)-PP(1,2))^2)));
%
% if Length(i)<J %for 2 segments,,,what if there are more than 2 segments
%
% C(g,i)= CCC(g,1);
%
% elseif Length(i)>J && Length(i)<2*J
% C(g,i)=CCC(g,2);
%
% end
%
% end
% end
Best Regards

4 commentaires

while Length(i)>J or Length(i)<2*J then assign c2 to the second segment
You could use
segment_index = c( max(1, ceil(Length / J)) )
This might, however, differ for the case where the Length entry is an exact multiple of J... which is a case your while does not handle.
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor le 21 Avr 2021
Modifié(e) : Pooneh Shah Malekpoor le 21 Avr 2021
Hello Walter,, thanks for this, however, sorry as I have editted my question as you can see .In fact, I have replaced while with if. How can i define an if condition for the above code such that it continues running for all of the segments(whose number is not clear)?
and what do you mean by max(1, ceil(Length / J)) here?
In the original form of your question, we could not guarantee that the Length entries were non-negative, so we could not guarantee that ceil(Length / J) would be > 0 . In such a situation then under the definition "if Length(i)<J then assign ccc1" we need to assign the first segment. Rather than testing for negative and using 1 in that case, we can use max(1,value) which will be 1 if value <= 1 and will be the value otherwise.
In the revised form we can see that the Length is a euclidean distance. Euclidean distances can potentially be 0, so we run into the same problem that Length/J might be 0, and ceil(0) is 0. Rather than testing for that case, we can use the max() as before.
Thank you so much, it is working now...I used the idea you gave me on ceil..
i=1:n
Length(i)=sqrt(abs((H(i,1)-PP(1,1)).^2+((H(i,3)-PP(1,2)).^2)));
z(i)=ceil(Lengthl(i)/J);
C(g,i) = CCC( g, z(i));

Connectez-vous pour commenter.

 Réponse acceptée

So you want to discretize your data?
x = rand(10, 1);
binedges = 0:0.25:1;
values = [1.1, 2.22, 3.333, 4.4444];
d = discretize(x, binedges, values);
results = table(x, d, 'VariableNames', ["x", "corresponding values"])
results = 10×2 table
x corresponding values _________ ____________________ 0.37963 2.22 0.44234 2.22 0.29959 2.22 0.88397 4.4444 0.061985 1.1 0.78041 4.4444 0.0083851 1.1 0.6224 3.333 0.66319 3.333 0.45249 2.22

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by