Differences Between Matlab and C/C ++/Java

Source: Internet
Author: User

This article is original by PurpleSword (jzj1993). Please indicate the variable

Definition and initialization
In C/C ++, you must first define the variable. If it is not initialized, its value is uncertain.
Before using variables in Java, you must initialize or assign values (global variables in the class are automatically initialized to the default value during instantiation). Variables in Matlab do not need to be pre-defined during writing, however, the multi-dimensional variables should have been defined during reading. To improve the performance, you can first define an empty multi-dimensional variable, such as a two-dimensional char array a = ['';''];
Array Length
In C, the array length is fixed. the malloc function can be used to expand the memory space and copy existing values.
The length in C ++/Java can be used to get new memory space and copy existing values.
The length and dimension of variables in Matlab can be automatically expanded during writing. To improve performance, it is best to avoid automatic expansion of variable latitude,
Instead, empty multidimensional variables are pre-defined, such as two-dimensional char array a = ['';'']. Note: automatic expansion of the Matlab variable length only occurs when the variable is written, when reading a variable, it cannot be accessed out of bounds. For example, for the defined variable a = [1 2; 3 4], the value assignment statement a (5, 5) = 1 will automatically expand, B = a (10, 10), the access will be out of the border and an error occurs.
Array comparison
C language: for strings, strcmp functions are available. For other types, compare them one by one in a loop,
Matlab: The strcmp function is available for strings. An array of any type with the same latitude and length can be directly compared with =. The comparison result is an array of the same size. If it is 1, the values at the corresponding position are equal, and if it is 0, they are not equal.
Auto-increment operation
C/C ++/Java x ++ is available;
Matlab can only be x = x + 1;
Array subscript
C language: subscript starts from 0, and brackets indicate a [0]; two-dimensional array a [0] [1] Matlab: subscript starts from 1, and parentheses indicate a (1 ); two-dimensional array a (1, 2)
String Representation
Double quotation marks ("test") in C Language"
Single quotation marks 'test' for Matlab'
Common Methods for string Replication
// In C, the equal sign assignment cannot be used. The equal sign assignment is only a copy of the pointer.
C language: strcpy (a, B );
// C ++/Java can use the overload operator "=" to copy strings with equal signs
C ++/Java: String B = new String ();
% Matlab depends on the situation, B = a can copy the entire string variable
% If B is a two-dimensional char array, B (2, 1: length (a) =;
% B's length is not enough and will automatically expand. If a's length is not enough, it will fill in spaces.
Matlab: B = a; B (2, 1: length (a) =;

Operator
Remainder
C language: a % B
Matlab: mod (a, B)
Not equal
C language :! =
Matlab :~ =

Circular writing
For (I = 0; I <n; ++ I) {// I = 0 ~ N-1}
For I = 1: n % I = 1 ~ Nend

Condition Branch Structure
If (a = 0) {} else if (a = 1) {} else {}
If a = 0 elseif a = 1 elseend

Function Definition
C language: a function can have only one return value. Multiple return values can be passed into the function by defining the struct and using the pointer as the parameter. Matlab: the function can have multiple return values through [a B] = func () obtain

Code optimization
Matlab uses matrix transformation as much as possible, rather than loops.

This article by PurpleSword (jzj1993) original, reproduced please indicate the original web site http://blog.csdn.net/jzj1993/article/details/18987073

Related Article

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.