# Counting numbers in a sequence while corrosponding to another sequence

4 visualizzazioni (ultimi 30 giorni)
oli8819 il 7 Giu 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Hugo il 7 Giu 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 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Hugo il 7 Giu 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 il 7 Giu 2013
Brillaint thanks!

Accedi per commentare.

### Più risposte (2)

Image Analyst il 7 Giu 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 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
oli8819 il 7 Giu 2013
Unfortunately, I don't have the Image Processing toolbox due to being on a basic licence but thanks anyway.

Accedi per commentare.

Andrei Bobrov il 10 Giu 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))];
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by