Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use tabulate and intersect functions in MATLAB

Shulou Source: shulou.com Published: 2022-06-01 06:30:27 10月04日 Update

This article mainly shows you "how to use tabulate and intersect functions in MATLAB", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "how to use tabulate and intersect functions in MATLAB" this article.

Count the number and frequency of each element in a vector

1. Function: tabulate

two。 Usage: table= tabulate (x)

Where x is a vector (rows, columns), the returned table has three columns, the first column is all the non-repeating elements in the vector x, the second column is the number of occurrences of each element in the first column, and the third column is the frequency of each element in the first column.

3. Example

> > x = [1 2 3 1 5 6 3 2 4 2 3 1]

> > table = tabulate (x)

Table =

1 3 25

2 3 25

3 3 25

4 1 8.3333

5 1 8.3333

6 1 8.3333

> >

In addition:

There are also two built-in functions called histcount / histcounts2 that can be used to count the number, but it is used to count the number of values that fall in a certain interval, so I won't elaborate on it here.

How to judge that one set is a subset of another

I didn't find a ready-made built-in function, but I can do it with a built-in function and an if statement.

1. Related function: intersect

two。 Usage: C = intersect (A, B)

Where An and B are two vectors, and the returned C is the intersection of An and B.

3. Example

> > intersect ([1 2 3], [1 3 5 6 9])

Ans =

1 3

> > intersect ([2 36], [1 3 5 6 9 2])

Ans =

2 3 6

> >

If I want to judge whether An is a subset of B, then the number of elements of A must be less than or equal to the number of elements of B. if the number of elements of C is equal to the number of elements of A, then A must be a subset of B. (the number of elements of the intersection of An and B is the same as the number of A, and An is a subset of B.)

% wants to determine whether An is a subset of B.

Assuming that after the previous processing, A must have fewer elements than B. now it is like knowing whether all the elements in An are in B.

Co = intersect (A, B);% Public part

If length (co) = = length (A)

Disp ('An is a subset of B')

Else

Disp ('An is not a subset of B')

End

In addition:

In fact, there is another function that can also be used: C=ismember (A _ Magi B), which can take two vectors A _ Magi B as parameters to determine whether the elements in A vector are in B vector. Finally, C is a logical vector, and the length of C is equal to the length of A. You can sum the return value C and determine whether the sum is equal to the number of elements of the A vector. If it is equal, then the A vector is a subset of the B vector. I won't introduce it in detail.

These are all the contents of the article "how to use tabulate and intersect functions in MATLAB". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Element vector function number subset two content that is article statistics intersection number length frequency learning help affirmation interval parameter numerical value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Shulou Technology Linux OPPO Reno