unclear number of if or maybe other commands?!
Afficher commentaires plus anciens
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
Walter Roberson
le 21 Avr 2021
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
le 21 Avr 2021
Modifié(e) : Pooneh Shah Malekpoor
le 21 Avr 2021
Walter Roberson
le 21 Avr 2021
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.
Pooneh Shah Malekpoor
le 21 Avr 2021
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Numeric Types dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!