X./a divide every element of X by a (piecewise) A.*B element vise multiplication of A and B (both same size) format long; format short; a = pi; sprintf('%0.3f', a) => 3.142 size(A, 1) size(A, 2) length(A) size of longest dimension who / whos what variables I have in memory right now load('featuresX.mat') clear XVariable clear that specific variable save hello.mat v; save variable v as mat file save hello.txt v -ascii; human readble form A = [1 2; 3 4; 5 6]; A([1 3], :) Specific indexing: rows with index 1 and 3 , all columns 1 2 ; 5 6 A( :, 2) = [10; 11; 12] assign to a range log(A) elemen-vise log; exp(A) element-vise abs(A) -A A + 1 (element-vise) add one to all A < 3 element-vise operation (results in a matrix of zeros and ones) find(A<3) returns indices of elements that satisfy that [r c] = find(A <= 7) r row, c column sum(A) prod(A) product of elements. floor/ceil max (rand(3) rand(3)) elemnt-vise max max(A, [], 1) max of columns max(A, [], 2) max of rows max(max(A)) OR max(A(:)) global maximum A(:) turns A to a column matrix A = magic(9) sum(A, 1) column-vise sum sum(A, 2) row-vise sum sum(sum(A .* eye(9))) lement-vise multiplication of diagonals to get the diagon and sum flipud flip upside down t = [0:0.01:4]; y1 = sin(2 * pi * t); plot(t, y1); hold on; y2 = cos(2 * pi * t); plot(t, y2, 'r'); xlabel('Time'); ylabel('value'); legend('sin', 'cos'); title('My Plot'); print -dpng 'myplot.png'' help plot to save other formats figure(1); plot(t, y1); figure(2); plot(t, y2); two figures subplot(1,2, 1); plot(t, y1); subplot(1,2, 2); plot(t, y2);% Divides plot a 1*2 grid and access the 1st subplot axis(0.5 1 -1 1) set range of plot clf; % clear figure imagesc(magic(9)); imagesc(magic(15)) , colorbar , colormap gray for/if/while/function
addpath('/home/morteza/'); add path fopr matlab to lookup function [y1, y2] = squareandcube(x) y1 = x^2 y2 = x^3 [a, b] = squareandcube(5) vectorization (much more efficient! than for loops sigmas etc) Σ_i θ(i) X(i) -> X' θ -------------------------- close all; clear; clc indexes start at 1. Add comments using the percent (%) symbol. a = 1a = [1 2 3; 4 5 6; 7 8 10] matrix, each row separated by semi-colon Another way to create a matrix is to use a function, such as ones, zeros, or rand. For example, create a 5-by-1 column vector of zeros. z = zeros(5, 1) 5 rows, 1 column Apply a single arithmetic function on all elements of a matrix a + 10 will add 10 to all elements of a sin(a) To transpose (use apastrophy character) a' To inverse inv(a) a * inv(a) is identity matrix To display more decimal
digits format long format short To perform element-wise multiplication rather than matrix multiplication, use the .* operator:p = a.*a will multiple element by its associative element. You can also use: a.^3 multiplication, division, and power can operate element-wise in this format. Concatenation In fact, you made your first array by concatenating its individual elements. The pair of square brackets [] is the concatenation operator. [a a] will concatenate a to itself in a row. is equal to [a, a] [a ; a] will concatenate a to itself in a column fashion (one on top of another) Complex numbers: To represent the imaginary part of complex numbers, use either i or j. c = [3+4i, 4+3j, -i, 10j] Array Indexing 4-by-4 magic square A = magic(4) random generation: rand(3,5,2); provide row and column number: A(4, 2) = 14 Less common, but sometimes useful, is to use a single subscript that traverses down each column in order:A(8)=14 Index out of bound: if the array is on right hand side of equal, you get error test = A(4,5) . if array is on left hand side, . The size of the array increases to accommodate the newcomers. A(4,5) =17 Range To refer to multiple elements of an array, use the colon operator, which allows you to specify a range of the form start:end. For example, list the elements in the first three rows and the second column of A: A(1:3, 2) The colon alone, without start or end values, specifies all
of the elements in that dimension. For example, select all the columns
in the third row of A: A(3, :) The colon operator also allows you to create an equally spaced vector of values using the more general form start:step:end.B = 0:10:100 0 10 20 .... Workspace variablessave myfile.mat load myfile.mat Strings'string' 'You''re right' concatenate strings just as you concatenate numbers to make a matrix ['hello world', ' - ' , 'you''re right' ] hello world - you're right functionsTo convert numeric values to strings, use functions, such as num2str or int2str. When there are multiple output arguments, enclose them in square brackets: [maxA,location] = max(A); To display a text message disp('message for you') clc to clear screen Plot x = 0 : pi/100 : 2*pi y = sin(x) plot(x, y) xlabel('x) ylabel('sin(x)') title('Plot of the Sine Function') By adding a third input argument to the plot function, you can plot the same variables using a red dashed line. plot(x,y,'r--') such as a +, o, or *. For example, 'g:*' requests a dotted green line with * markers. Notice that the titles and labels that you defined for the first plot are no longer in the current figure window. By default, MATLAB® clears the figure each time you call a plotting function, resetting the axes and other elements to prepare the new plot. x = 0:pi/100:2*pi; y = sin(x); plot(x,y) plot(X(:, 2), y, '*') hold on # add the next plots to current plot. figure; make a new window for the plot. y2 = cos(x); plot(x,y2,'r:') legend('sin','cos') 3D Plots Three-dimensional plots typically display a surface defined by a function in two variables, z = f (x,y). [X,Y] = meshgrid(-2:.2:2); create the base x, y Z = X .* exp(-X.^2 - Y.^2); calculate the function surf(X,Y,Z) to draw a surface plot Sub-plots You can display multiple plots in different subregions of the same window using the subplot function.For example, create four plots in a 2-by-2 grid within a figure window. t = 0:pi/10:2*pi; [X,Y,Z] = cylinder(4*cos(t)); subplot(2,2,1); mesh(X); title('X'); subplot(2,2,2); mesh(Y); title('Y'); subplot(2,2,3); mesh(Z); title('Z'); subplot(2,2,4); mesh(X,Y,Z); title('X,Y,Z'); edit portland creates a script file named portland.m n = 50; r = rand(n,1); plot(r) m = mean(r); hold on plot([0,n],[m,m]) a plot with two points (0,m) and (n,m) ::::: [0, n] is the x points hold off title('Mean of Random Uniform Data') To run the script, type its name at the command line: portland For loop, IF nsamples = 5; npoints = 50; for k = 1:nsamples currentData = rand(npoints,1); sampleMean(k) = mean(currentData); end if overallMean < .49 disp('Mean is less than expected') elseif overallMean > .51 disp('Mean is greater than expected') else disp('Mean is within the expected range') end ftp://ftp.mathworks.com/pub/customer_pickup/UT/ dataMatrix(matrix, rowname, columnname) -> bundle all data together, can be used to show heat map order of applying filters matters. they depend on percentile, but it seems like there is no standard saying you should apply this first and then the next filter on the result of first filter .... on the editor click evaluate cell and advance %% Heirarchical Clustering of profiles %% makes cells publish icon beside the print icon, runs a file cell by cell and makes a report containing outputs and figures clc to make gui type > guide matlab graph theory multialign : multiple sequence alignmrnt bioindexedfile : for large data files, index based on a column large file : memory map file, memory map object. featuresmap fastQ format tic toc - exec time vectorization-Techniques for Improving Performance matlab is column major blas lapak spline curve fitting profiler to give a report of execution time textscan is faster than fscanf additional process: parfor-distributed task job optimset('useparallel','always'); Optimization toolbox matlabpool open //assign number of matlab workers (duplicates of matlab thread message passing to use recursive parfor data parallel code parallel rand(4, codistributor) |