필터 지우기
필터 지우기

Counting numbers in a sequence while corrosponding to another sequence

조회 수: 2 (최근 30일)
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

채택된 답변

Hugo
Hugo 2013년 6월 7일
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
Hugo
Hugo 2013년 6월 7일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2013년 6월 7일
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
oli8819
oli8819 2013년 6월 7일
Unfortunately, I don't have the Image Processing toolbox due to being on a basic licence but thanks anyway.

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2013년 6월 10일
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))];

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by