Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
СКМ_Массивы.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
179 Кб
Скачать
☆

Reshape

Reshape array

B = reshape(A,m,n) returns the m-by-n matrix B whose elements are taken column-wise from A. An error results if A does not have m*n elements.

B = reshape(A,m,n,p,...) or B = reshape(A,[m n p ...]) returns an n-dimensional array with the same elements as A but reshaped to have the size m-by-n-by-p-by-.... The product of the specified dimensions, m*n*p*..., must be the same as prod(size(A)).

B = reshape(A,...,[],...) calculates the length of the dimension represented by the placeholder [], such that the product of the dimensions equals prod(size(A)). The value of prod(size(A)) must be evenly divisible by the product of the specified dimensions. You can use only one occurrence of [].

B = reshape(A,siz) returns an n-dimensional array with the same elements as A, but reshaped to siz, a vector representing the dimensions of the reshaped array. The quantity prod(siz) must be the same as prod(size(A)).

Examples

Reshape a 3-by-4 matrix into a 2-by-6 matrix.

A =

1 4 7 10

2 5 8 11

3 6 9 12

B = reshape(A,2,6)

B =

1 3 5 7 9 11

2 4 6 8 10 12

B = reshape(A,2,[])

B =

1 3 5 7 9 11

2 4 6 8 10 12

Squeeze

Remove singleton dimensions

B = squeeze(A) returns an array B with the same elements as A, but with all singleton dimensions removed. A singleton dimension is any dimension for which size(A,dim) = 1. Two-dimensional arrays are unaffected by squeeze; if A is a row or column vector or a scalar (1-by-1) value, then B = A.

Consider the 2-by-1-by-3 array Y = rand(2,1,3). This array has a singleton column dimension — that is, there's only one column per page.

Y =

Y(:,:,1) = Y(:,:,2) = Y(:,:,3) =

0.5194 0.0346 0.5297

0.8310 0.0535 0.6711

The command Z = squeeze(Y) yields a 2-by-3 matrix:

Z =

0.5194 0.0346 0.5297

0.8310 0.0535 0.6711

Объединение множеств

union

Find set union of two vectors

c = union(A, B) returns the combined values from A and B but with no repetitions. In set theoretic terms, c = A B. Inputs A and B can be numeric or character vectors or cell arrays of strings. The resulting vector is sorted in ascending order.

c = union(A, B, 'rows') when A and B are matrices with the same number of columns returns the combined rows from A and B with no repetitions.

[c, ia, ib] = union(...) also returns index vectors ia and ib such that c = a(ia) b(ib), or for row combinations, c = a(ia,:) ? b(ib,:). If a value appears in both a and b, union indexes its occurrence in b. If a value appears more than once in b or in a (but not in b), union indexes the last occurrence of the value.

a = [-1 0 2 4 6]; b = [-1 0 1 3];

[c, ia, ib] = union(a, b);

c =

-1 0 1 2 3 4 6

ia =

3 4 5

ib =

1 2 3 4

Пересечение множеств

Intersect

Find set intersection of two vectors

c = intersect(A, B) returns the values common to both A and B. In set theoretic terms, this is A[[INTERSECT]] B. Inputs A and B can be numeric or character vectors or cell arrays of strings. The resulting vector is sorted in ascending order.

c = intersect(A, B, 'rows') when A and B are matrices with the same number of columns returns the rows common to both A and B.

[c, ia, ib] = intersect(a, b) also returns column index vectors ia and ib such that c = a(ia) and c = b(ib) (or c = a(ia,:) and c = b(ib,:)).

A = [1 2 3 6]; B = [1 2 3 4 6 10 20];

[c, ia, ib] = intersect(A, B);

disp([c; ia; ib])

1 2 3 6

1 2 3 4

1 2 3 5