Some of the most recently used operations of MATLAB

Source: Internet
Author: User
Tags add numbers rand

Recently in writing MATLAB program, some commonly used functions and expression methods often find, for later convenience, special record


1-nnz the number of non-0 elements in the lookup matrix:

n = Nnz (X) returns the number of nonzero elements in matrix X.


2-prod the product of a calculated element

B = PROD (a) returns the product of the array elements of A.


3-fftshift/ifftshift, move the 0 frequency to the center of the spectrum

Y = Fftshift (X) rearranges the outputs of the FFT, fft2, and fftn by moving the Zero-frequency component to the center of the Array.

Ifftshift (X,dim) applies the ifftshift operation along the dimension dim.

The difference between fftshift and Ifftshift are important for input sequences of odd-length.

N = 5;

X = 0:n-1;
Y = Fftshift (Fftshift (X));
Z = Ifftshift (Fftshift (X));
Notice. Z is a correct replica of X.
IsEqual (x, y), isequal (x,z)
When processing the frequency signal, the FFT can be fftshift,ifft before the ifftshift can be performed.
4-squeeze: Removing dimensions of size 1
b = Squeeze (A) returns an array B with the same elements as A, but with all singleton dimensions. A Singleton dimension is any dimension for which size (A,dim) = 1.
For example, Rand (4,1,3) produces an evenly distributed array of 3 pages, 4 rows and 1 columns per page, and after squeeze, the dimension of the 1 column is gone, leaving only a two-dimensional array of 4 rows and 3 columns. RAND (4,2,3) does not change after all squeeze because there are no 1 or 1 rows of dimensions.
5-sum, seeking accumulation and
S = SUM (A) returns the sum of the elements of A along the first array dimension whose size does not equal 1:
S = SUM (A,dim) sums the elements of A along dimension Dim. The dim input is a positive integer scalar.
When S=sum (A); By default, the 1-koriyuki vector is added. S=sum (a,2) according to the 2-Willi Vector Plus
Sum of Elements in each Column
Create a 3-by-3 matrix.
A = [1 3 2; 4 2 5; 6 1 4]
A =
1 3 2
4 2 5 6
1 4
Compute the sum Of the elements in each column.
s = SUM (A)
s =
11 6 11
6-ceil:round Toward positive Infinity
For example:
X = [ -1.9-0.2 3.4; 5.6 7 2.4+3.6i];
y = ceil (X)
y =


-1.0000 + 0.0000i 0.0000 + 0.0000i 4.0000 + 0.0000i
6.0000 + 0.0000i 7.0000 + 0. 0000i 3.0000 + 4.0000i
Can also be a property value
6-FLIPLR (x) returns x with row preserved and columns flipped in the left/right direction.
The row remains unchanged as the column is flipped, for example,
<span style= "FONT-SIZE:18PX;" >a=magic (4)

a =     2     3 5 each     8
     9 7 6 a 4     1

k>> B=FLIPLR (a)

B =     3     2
     8    11     5     6     7     9
     1     4</span>
7--flipud: = Flipud (a) returns a with it rows flipped in the up-down direction (that's, about a horizontal axis).
For example:
Flip Multidimensional Array

Create a multidimensional array.

A = Cat (3, [1 2; 3 4], [5 6; 7 8])
A (:,:, 1) =
 
     1     2
     3     4


A (:,:, 2) =

     5     6
     7     8

A is an array of size 2-by-2-by-2.

Flip the elements on each page of A in the vertical direction.

B = Flipud (A)
B (:,:, 1) =

     3     4
     1     2


B (:,:, 2) =

     7     8
     5     6
8-tril:lower triangular part of the matrix
L = Tril (x,k) returns the elements on and below the kth diagonal of X. k = 0 is the main diagonal,k > 0 is above the MA In diagonal, and K < 0 are below the main diagonal.
Triu:u = Triu (x,k) returns the element on and above the kth diagonal of X. k = 0 is the main diagonal, K > 0 is above t He main diagonal, and K < 0 is below the main diagonal.
9-matlab already has a lot of wavelet functions, such as Meyer Wavelet,
% Set effective support and grid parameters. 
LB =-8; UB = 8; n = 1024x768; 

% Compute and plot Meyer wavelet and scaling functions. 
[Phi,psi,x] = Meyer (lb,ub,n); 
Subplot (211), Plot (x,psi) 
title (' Meyer wavelet ') 
subplot (212), Plot (X,phi) 
title (' Meyer Scaling function ')
10-title, you can add numbers or variables,
Figure (+), for
i=1:10
subplot (2,5,i), Imshow (X (:,:, i)), title ([' i = ', Num2str (i)])
End
Implementation uses a title
The use of 11-function handle,
Function handles can give you access to functions you might is able to execute. For instance, with a handle you can call a function even if it's no longer on your matlab®path. You can also call a local function from outside of the file, defines that function.
Handle = @functionname
Handle = @ (arglist) anonymous_function
12-matlab convolution operation Conv Conv2
13-PSF2OTF: Onvert point-spread function to optical transfer functions.
OTF = PSF2OTF (PSF) computes the Fast Fourier transform (FFT) of the Point-spread function (PSF) array and creates the Opti Cal transfer function Array, OTF, that's not influenced by the PSF off-centering. By default, the OTF array is the same size as the PSF array.

The FFT transform of the PSF array is computed and an OTF array is obtained.

PSF can is any nonsparse, numeric array. OTF is of class double

Here's a look at the PSF2OTF.M function. The main concern is how the function realizes the change of the size of the PSF.

We are using the form of OTF = PSF2OTF (psf,outsize). The same parseinputs function exists in PSF2OTF.M for verifying parameters and assignment variables.

[PSF, Psfsize, outsize] = Parseinputs (varargin{:});

This function assigns a value to psf,psfsize,outsize three variables respectively. If the PSF is not all 0, execute the following statement:

Padsize = outsize-psfsize;

Padsize indicates the size that needs to be filled, it is more intuitive to understand, and the PSF is expanded into a matrix as large as outsize. For example, outsize is [100,100],psfsize[10,10], then Padsize is [90,90].

PSF = Padarray (PSF, Padsize, ' post ');

The sentence represents a fill image or fills an array, and the parameter does not contain Padval, which is populated with 0 by default, and ' post ' for direction, which means padding after each dimension. For example, the matrix filled to 5*5 for [1, 2;3, 4] is the following effect:

1 2 0) 0 0

3 4 0) 0 0

0 0 0) 0 0

0 0 0) 0 0

0 0 0) 0 0

PSF = Circshift (Psf,-floor (PSFSIZE/2));

Next, the new matrix is periodically translated Circshift, where-floor (PSFSIZE/2) represents the direction of the translation, divided into portrait and landscape. Continuing with the above example, the translation direction matrix is [ -1,-1] and the following results are obtained:

4 0 0) 0 3

0 0 0) 0 0

0 0 0) 0 0

0 0 0) 0 0

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.