# All Questions

**109**

votes

**9**answers

15223 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...

**45**

votes

**6**answers

19420 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...

**278**

votes

**9**answers

49681 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...

**37**

votes

**3**answers

62938 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. ...

**42**

votes

**4**answers

12513 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.
...

**140**

votes

**14**answers

41907 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...

**90**

votes

**2**answers

18627 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...

**27**

votes

**1**answers

1236 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...

**-2**

votes

**1**answers

611 views

### How to put these images together?

I have 200 time points. For each time point, there is an image, the size of which is 40*40 double, corresponds to this time point. For example, image 1 corresponds to time point 1; image k corresponds...

**110**

votes

**4**answers

21524 views

### Is MATLAB OOP slow or am I doing something wrong?

I'm experimenting with MATLAB OOP, as a start I mimicked my C++'s Logger classes and I'm putting all my string helper functions in a String class, thinking it would be great to be able to do things li...