MATLAB remodeling and expansion Matrix

Source: Internet
Author: User
Document directory
  • Extended matrix size
  • Zoom out Matrix
  • Remodeling matrix shape
  • Pre-specified memory space

Extended matrix size

Expand the size of any existing matrix so that it is still a rectangle. For example, a matrix of 4 to 3 is connected to a matrix of 7 to 3, because each row has three columns.

Two methods to expand the matrix size:

Connect new elements to the current Matrix

Store data outside the current Matrix

Note: If you want to constantly expand the matrix size to require more space (usually in a loop), it is best to pre-define the size of the matrix when you first define it.

Connection Matrix

Connection is a very useful operation when you want to expand a matrix by adding elements or modules that match the size of the original matrix. This means that the sizes of all matrices to be connected are equal in the join dimension.

For example, use a user-defined function compareresults to operate the data stats04 and stats03 in The Matrix. Each cycle stores the result data of the function at the end of comp04.

Col = 10;

Comp04 = [];

For k = 1:50

T = compareresults (stats04 (k, 1: COL), stats03 (k, 1: COL ));

Comp04 = [comp04; t];

End

Connection struct or CELL ARRAY

The operations for connecting to a struct array or cell are similar to those for operating a common matrix.

For example, generate a 3x8 struct matrix S, each of which has the following fields: X, Y, and Z. Connect the second structure array S2.

Generate a 3-by-8 structure matrix s:

For k = 1:24

S (K) = struct ('x', 10 * k, 'y', 10 * k + 1, 'z', 10 * k + 2 );

End

S = reshape (s, 3, 8 );

Generate the second 3-by-2 matrix using the same domain name:

For k = 25:30

S2 (k-24) = struct ('x', 10 * k, 'y', 10 * k + 1, 'z', 10 * k + 2 );

End

S2 = reshape (S2, 3, 2 );

Connect S2 horizontally to S

S = [s s2]

S =

3x10 struct array with fields:

X

Y

Z

Add a small block to the matrix

When dimensions do not match, you can simply store the elements outside the matrix boundary to add one or more elements to a matrix. MATLAB automatically keeps the matrix extension matrix rectangular.

Construct a 3-by-5 matrix and add a new element using the concatenation method. The operation fails because a single column matrix cannot be connected to a matrix with five columns of elements.

A = [10 20 30 40 50 ;...

60 70 80 90 100 ;...

110 120 130 140 150];

A = [A, 160]

??? Error Using ==> vertcat

All rows in the bracketed expression must have the same

Number of columns.

Retry. Another method is used to enable MATLAB to automatically match the matrix size. Store the new element in the fourth row, which does not exist in the current matrix. MATLAB will expand matrix A, add a new row, and set the elements in the second column to the Fifth Column to 0.

A (4, 1) = 160

A =

10 20 30 40 50

60 70 80 90 100

110 120 130 140 150

160 0 0 0 0

Note: If you try to read a location that does not exist in the matrix, an error is returned. Only data can be written.

Similarly, you can add a matrix instead of a single element to expand the matrix.

A (,) = magic (3) + 100

A =

10 20 30 40 50

60 70 80 90 100

110 120 130 140 150

108 101 106 0 0

103 105 107 0 0

104 109 102 0 0

The matrix element does not need to be added consecutively. MATLAB automatically fills the 0 element so that the matrix is still kept as a rectangle when new elements are added anywhere.

A (4, 8) = 300

A =

10 20 30 40 50 0 0

60 70 80 90 100 0 0 0

110 120 130 140 150 0 0 0

0 0 0 0 0 300

Extends a structure or cell matrix.

It is the same as the normal operation matrix. MATLAB fills in an empty cell ([]) to keep it as a rectangle.

C = {'madison ', 'G', [5 28 1967];...

46, '1970 maple Dr ', 325}

Add a cell element to c {3, 1}, and Matlab will automatically expand the C matrix:

C {3, 1} =...

Struct ('Fund _ a',. 45, 'Fund _ E',. 35, 'Fund _ G', 20 );

C =

'Madison ''g' [1x3 double]

[46] '1970 maple Dr '[3.0153e + 003]

[1x1 struct] [] []

Extended character matrix

Operations are the same as normal matrices, but we do not recommend using this method to expand matrices. MATLAB fills the uninitialized position with 0, but both MATLAB and other programming languages regard 0 as the termination of the string, so sometimes it seems to be shorter when some functions process extended strings.

For example, extend a 5-character string to 12 characters. The initial result looks like a normal string.

Greeting = 'hello'; greeting (1, 8: 12) = 'World'

Greeting =

Hello World

After converting the string, we can see that the extension point is the character Terminator.

Uint8 (greeting)

Ans =

72 101 108 108 111 0 0 87 111 114 108

This will lead to the use of some functions, such as strcmp, which will produce some unexpected results.

Strcmp (greeting, 'Hello World ')

Ans =

0

Zoom out Matrix

You can assign an empty matrix value [] to delete the rows and columns of the matrix.

Example: A = magic (4)

A =

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

Delete the second column:

A (:, 2) = []

Then the matrix is changed:

A =

16 3 13

5 10 8

9 6 12

4 15 1

If a single element in the matrix is deleted, the result is no longer a matrix.

Example: A (1, 2) = []

An error is returned. However, linear indexes can be used to delete a single element or a sequence element. This is where the matrix will become a row vector:

A () = []

A =

16 9 3 6 13 12 1

Remodeling matrix shape

Reshape: Modify the matrix shape;

Rot90: rotate the matrix 90;

Fliplr: left/right flip;

Flipud: Flip up and down;

Flipdim: flipped along a specified dimension;

Transpose: Flip the matrix along the main diagonal line to convert the row vector into a column vector, and vice versa;

Ctranspose;

Example:

Reshape the matrix: Change the 3-by-4 matrix to 2-by-6

A = [1 4 7 10; 2 5 8 11; 3 6 9 12]

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

Ø transpose matrix (. 'is equivalent to transpose;' is equivalent to ctranspose)

B = .'

B =

1 2 3

4 5 6

7 8 9

10 11 12

A = [1 + 9i 2-8i 3 + 7i; 4-6i 5 + 5I 6-4i]

A =

1.0000 + 9.20. I 2.0000-8.20. I 3.0000 + 7.20. I

4.0000-6.20. I 5.0000 + 5.20. I 6.0000-4.20. I

B ='

B =

1.0000-9.20. I 4.0000 + 6.20. I

2.0000 + 8.20. I 5.0000-5.20. I

3.0000-7.20. I 6.0000 + 4.20. I

Ø rotation matrix

B = rot90 ()

B =

10 11 12

7 8 9

4 5 6

1 2 3

Ø flip Matrix

B = fliplr ()

B =

10 7 4 1

11 8 5 2

12 9 6 3

Pre-specified memory space

The size of the repeated expansion matrix (for example, adding elements to the matrix every time in a loop) will affect the running efficiency of the program. This is because:

Ø MATLAB requires time to allocate memory each time it increases the matrix size.

The memory space of the newly specified inner is not consecutive, so all operations on this matrix will be slowed down.

Therefore, it is best to estimate the maximum size of the matrix and allocate the memory space in advance when the matrix is generated. In this way, the program runs on a continuous memory block.

For example, predefine a 25,000x10,000 matrix and initialize each element as 0:

A = zeros (25000,100 00 );

Construct a pre-defined matrix

When the maximum size of the estimated matrix is pre-defined memory space, data can be stored in the matrix as needed.

For example, predefine a large matrix to read data from the file to this matrix until the end of the file:

Blocksize = 5000;

Maxrows = 2500000; Cols = 20;

Rp = 1; % row indicator

% Predefines A, the maximum possible size

A = zeros (maxrows, cols );

% Open the file and store the file pointer

FID = fopen ('statfile. dat ', 'R ');

While true

% Read the file into a cell array. End with a line break.

Block = textscan (FID, '% n', blocksize * Cols); % file, format, and format usage

If isempty (Block {1}) Break, end;

% Convert the cell array into a matrix, reshape, and assign

A (RP: RP + blocksize-1, 1: Cols) =...

Reshape (cell2mat (

Block), blocksize, cols );

% Process the data in.

Evaluate_stats (a); % User-Defined Function, skipped

% Update row indicator

Rp = RP + blocksize;

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.