Matlab learning notes (3) matrix and cube

Source: Internet
Author: User

Iii. Subscript:

Column J of row I of matrix A is represented as a (I, j ). That is, a (2, 4) indicates the elements in the 2nd columns of 4th rows.

Therefore, the sum of the elements in column 4 can be calculated as follows:

A () + a (2, 4) + a (3, 4) + a (4, 4)

This will generate:

Ans =

34

However, this is not the best way to solve the sum of elements in a column.

Of course, you can use a subscript to describe data members, such as a (k). This is mainly used to indicate the k elements of a row vector or column vector. But it can also be applied to two-dimensional arrays. On the one hand, a matrix can be described as a long column vector composed of the columns of the original matrix, so a (8) can represent

A (4, 2 ).

If you try to read the elements of a (), an error will occur because the subscript cannot cross-border.

In other words, if you access an element outside the original matrix, the size is automatically adjusted to a new order.

For example:

X =;

X (4, 5) = 17

Will generate:

X =

16 3 2 13 0
5 10 11 8 0
9 6 7 12 0
4 15 14 1 17

 

4. colon (:) Operator

The colon operator is a particularly important operator in MATLAB. It appears in MATLAB in different forms.

Expression:

1: 10

Represents a group of row vectors composed of integers from 1 to 10.

1 2 3 4 5 6 7 8 9 10

To get a non-unit space and create a growth volume, you can:

100:-7:50

Indicates:

100 93 86 79 72 65 58 51

At the same time

0: PI/4: pi

Indicates:

0 0.7854 1.5708 2.3562 3.1416

The subscript expression introduces the colon operator to determine the position of the matrix:

A (1: K, j): 1 to k elements in column J of matrix

Sum (A (, 4): calculates the sum of the elements in the fourth column.

But this is not the best. The colon itself represents all elements of a certain row vector or a certain column vector in the matrix. At the same time, the keyword end indicates the last column or the last row.

Therefore:

Sum (A (:, end): calculates the last column of matrix.

Ans =

34

Why is the magic and value of the 4*4 cube 34?

Because the integers from 1 to 16 are evenly divided into four groups with the following values:

Sum (1:16)/4

Result:

Ans =

34

 

5. Magic function:

MATLAB has a built-in function to build a cube of any size. This function is magic.

B = magic (4)

Will generate:

B =

16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1

This matrix is roughly the same as matrix A. The only difference is the sequence of the second and third columns.

To change matrix B to A, that is, to swap the two columns in the middle

A = B (:, [1, 3, 2, 4])

That is to say, each row of B is rearranged in the order of 1, 3, and 2, and the result is:

A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

 

This section describes the following topics:

1. Underlying applications

2. Add new elements to the existing matrix

3. Colon Operator

4. End keyword

5. How to rearrange the matrix order

 

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.