# All Questions

15558 views

### Using i and j as variables in Matlab

i and j are very popular variable names (see e.g., this question and this one). For example, in loops: for i=1:10, % do something... end As indices into matrix: mat( i, j ) = 4; Why shoul...
19912 views

### Why is 24.0000 not equal to 24.0000 in MATLAB?

I am writing a program where I need to delete duplicate points stored in a matrix. The problem is that when it comes to check whether those points are in the matrix, MATLAB can't recognize them in the...
67136 views

### Subscript indices must either be real positive integers or logicals, generic solution

The following error occurs quite frequently: Subscript indices must either be real positive integers or logicals I have found many questions about this but not one with a really generic answer. ...
50977 views

### How can I index a MATLAB array returned by a function without first assigning it to a local variable?

For example, if I want to read the middle value from magic(5), I can do so like this: M = magic(5); value = M(3,3); to get value == 13. I'd like to be able to do something like one of these: value...
12982 views

### Generate a matrix containing all combinations of elements taken from n vectors

This question pops up quite often in one form or another (see for example here or here). So I thought I'd present it in a general form, and provide an answer which might serve for future reference. ...
42734 views

### Why is MATLAB so fast in matrix multiplication?

I am making some benchmarks with CUDA, C++, C#, and Java, and using MATLAB for verification and matrix generation. But when I multiply with MATLAB, 2048x2048 and even bigger matrices are almost insta...
18897 views

### arrayfun can be significantly slower than an explicit loop in matlab. Why?

Consider the following simple speed test for arrayfun: T = 4000; N = 500; x = randn(T, N); Func1 = @(a) (3*a^2 + 2*a - 1); tic Soln1 = ones(T, N); for t = 1:T for n = 1:N Soln1(t, n) = F...
1327 views

### Linear indexing, logical indexing, and all that

We are used to different forms of indexing in Matlab: standard (using integers along each dimension), logical (using logical values), linear (using a single index to traverse an array with more than...