## Plotting graph using for loop in MATLAB

Why this happens… With plot(x(i),y) you are plotting 100 single points (one in each iteration) and they are not shown by default. Therefore the plot looks empty. Solution 1: Vectorized calculation and direct plot I assume you meant to draw a continuous line. In that case no for-loop is needed because you can calculate and … Read more

## Multiply a 3D matrix with a 2D matrix

As a personal preference, I like my code to be as succinct and readable as possible. Here’s what I would have done, though it doesn’t meet your ‘no-loops’ requirement: for m = 1:C Z(:,:,m) = X(:,:,m)*Y; end This results in an A x D x C matrix Z. And of course, you can always pre-allocate … Read more

## What does operator “dot” (.) mean?

The dot itself is not an operator, .^ is. The .^ is a pointwise¹ (i.e. element-wise) power, as .* is the pointwise product. .^ Array power. A.^B is the matrix with elements A(i,j) to the B(i,j) power. The sizes of A and B must be the same or be compatible. C.f. “Array vs. Matrix Operations”: … Read more

## How do I get the second return value from a function without using temporary variables?

not that i know of. subsref doesn’t seem to work in this case, possibly because the second variable isn’t even returned from the function. since matlab 2009b it is possible to use the notation [~, b] = function(x) if you don’t need the first argument, but this still uses a temporary variable for b.

## Why does numpy std() give a different result to matlab std()?

The NumPy function np.std takes an optional parameter ddof: “Delta Degrees of Freedom”. By default, this is 0. Set it to 1 to get the MATLAB result: >>> np.std([1,3,4,6], ddof=1) 2.0816659994661326 To add a little more context, in the calculation of the variance (of which the standard deviation is the square root) we typically divide … Read more

## The meaning of colon operator in MATLAB

Actually a:b generates a vector. You could use it as index only because the (…) accepts a list also, e.g. octave-3.0.3:10> a = [1,4,7] a = 1 4 7 octave-3.0.3:11> b = [1,4,9,16,25,36,49] b = 1 4 9 16 25 36 49 octave-3.0.3:12> b(a) # gets [b(1), b(4), b(7)] ans = 1 16 49 Now, … Read more

## Access m-files in a subfolder without permanently adding it to the path

Add path to the top of MATLAB search paths for current MATLAB session only – addpath(PATHNAME) Same as addpath, but stays good for next sessions – savepath(PATHNAME) Add all subdirectories for current MATLAB session only – addpath(genpath(PATHNAME)) Note: One must be careful while adding paths because if there are multiple function files with the same … Read more

## Stable accumarray in MATLAB

We can use sortrows as a preprocessing step to sort the indices and corresponding values first, as its documentation states: SORTROWS uses a stable version of quicksort. As the subscripts in subs should be sorted with respect to their linear indices, we need to sort them in reverse lexicographic order. This can be achieved by … Read more

## How to divide an image into blocks in MATLAB?

Since blockproc (and the deprecated blkproc) are both functions in the Image Processing Toolbox, I thought I’d add a basic MATLAB solution that requires no additional toolboxes… If you want to divide a matrix into submatrices, one way is to use mat2cell to break the matrix up and store each submatrix in a cell of … Read more

## Matlab – PCA analysis and reconstruction of multi dimensional data

Here’s a quick walkthrough. First we create a matrix of your hidden variables (or “factors”). It has 100 observations and there are two independent factors. >> factors = randn(100, 2); Now create a loadings matrix. This is going to map the hidden variables onto your observed variables. Say your observed variables have four features. Then … Read more