Effacer les filtres
Effacer les filtres

Counting numbers in a sequence while corrosponding to another sequence

2 vues (au cours des 30 derniers jours)
oli8819
oli8819 le 7 Juin 2013
I am struggling to produce code for this problem where I am looking to count numbers in a sequence with their corrosponding intervals of another sequence. It is best shown wtih an example.
For example coloumn A is in thickness (m) and column B is in a sequence of 6 numbers (1-6). What I am trying to do is measure the thickness of each sequence in A with the corrsponding separate, seqeunce number in B (e.g 6 or 1) and how often they occur; then output a values for the thickness and frequency so they can be plotted on a histogram.
Column A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14] (metre intervals)
Column B=[6 6 6 6 4 4 4 2 2 6 6 4 4 4]
so for this sequence 6 occurs with a corrosponding thickness of 3m and 1m, 4 occurs wtih a thicknesses of 2m and 1m and 2 occurs with a thickness of 1m.
Any help would be much appreciated! Thanks

Réponse acceptée

Hugo
Hugo le 7 Juin 2013
You can do that using the following code
A=[1 2 3 4 5 6 8 9 10 11 12 13 14];
B=[6 6 6 6 4 4 4 2 2 6 6 4 4];
C=diff([0,B,0]);
D=(C~=0);
E=A(D(2:end))-A(D(1:end-1));
F=B(D(1:end-1));
A and B are your definitions of vectors.
The code first adds 0 at each side of B. The idea is to add a number that does not exist in B (which can be chosen as any number not in the output of unique(B)), so that the command diff() will detect any change in the value of the elements of B, and therefore signal the transition between intervals (which is formally done in D). Afterwards, the length of the interval is calculated by subtracting the value in A of the extremes of each interval (contained in E). F just keeps which number is in each interval.
By the way, notice that in your example, the 7 is missing from A, and it is not obvious whether the length of the first interval containing 4 should be 3 or 2.
Best regards
  4 commentaires
Hugo
Hugo le 7 Juin 2013
Let me clarify that
E contains the lengths of the intervals F contains what number was in that interval
In your examples, F(1) says that the first interval was composed of a sequence of 6's and E(1) says that the interval has length 4. In the same way, F(2) says that the second interval was composed of a sequence of 4's, and E(2) says that the interval has length 2. F(3) says that the third interval was composed of 6's and E(3) says that the interval has length 5.
Is that clear now?
oli8819
oli8819 le 7 Juin 2013
Brillaint thanks!

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 7 Juin 2013
I'm not sure how column A was used in getting those results, but this is best done with regionprops() in the Image Processing Toolbox, if you have it:
ColumnB=[6 6 6 6 4 4 4 2 2 6 6 4 4]
ub = unique(ColumnB) % Get unique numbers.
for k = ub
measurements = regionprops(ColumnB== k, 'Area');
allAreas = [measurements.Area]-1;
fprintf('For %d, the area = ', k);
fprintf('%d, ', allAreas);
fprintf('\n\n');
end
In the command window:
ColumnB =
6 6 6 6 4 4 4 2 2 6 6 4 4
For 2, the area = 1,
For 4, the area = 2, 1,
For 6, the area = 3, 1,
  1 commentaire
oli8819
oli8819 le 7 Juin 2013
Unfortunately, I don't have the Image Processing toolbox due to being on a basic licence but thanks anyway.

Connectez-vous pour commenter.


Andrei Bobrov
Andrei Bobrov le 10 Juin 2013
A=[1 2 3 4 5 6 8 9 10 11 12 13 14];
B=[6 6 6 6 4 4 4 2 2 6 6 4 4];
t = [true;diff(B(:))~=0];
out = [B(t).',accumarray(cumsum(t),A(:),[],@(x)x(end)-x(1))];

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by