# All Questions

**112**

votes

**9**answers

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

**50**

votes

**6**answers

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

**38**

votes

**3**answers

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

**283**

votes

**9**answers

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

**44**

votes

**4**answers

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

**144**

votes

**14**answers

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

**90**

votes

**2**answers

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

**28**

votes

**1**answers

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

**-2**

votes

**1**answers

637 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

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