matlab array subsetting by indices - matlab

Let's say I have a 3x3x3 Matlab array with members 1 to 27
a=reshape(1:27, [3 3 3])
I would like to create a subset of this with a syntax like
b=a(range1,range2,range3)
where for range1=range2=range3=1:2 I would get the members b(1,1,1) and b(2,2,2). i.e
b= [1 14]
Is it possible to do this just with indexing and without any functions (e.g. diag)? Thanks...

It can be done with sub2ind function as follows:
b=a(sub2ind(size(a),range1,range2,range3))
ans: b=[1 14]

The indexing can be done using sub2ind,
a(sub2ind(size(a),[1:2],[1:2],[1:2]))
if you want to avoid all functions, you could calculated the linear indices yourself...

Related

Using parfor in MATLAB for indirect indexing of an array

I am currently trying to parallelize my script for runtime benefits.
My code includes a segment, which has the following form when represented in a very abstract way:
x=zeros(5,1);
y{1}=[1; 3; 5];
y{2}=[2; 4];
parfor i=1:2
x(y{i})= func(y{i});
end
So, I want to populate the indices of the variable x not sequentially, but in a parallel way. This gives me, however, the following error:
The variable x in a parfor cannot be classified.
The indices to be assigned are always disjoint (such as the example [1; 3; 5] and [2; 4]) i.e. no overwriting of entries will occur during the parallel run, which would have otherwise jeopardized the non-sequential processing.
Is there perhaps another way to reformulate this functionality?
I would use structs since output size is changing.
x=zeros(5,1);
y{1}=[1; 3; 5];
y{2}=[2; 4];
parfor i=1:2
temp{i}= sin(y{i});
end
for i=1:2
x(y{i})=temp{i};
end

Matrix indexing using vectors in Matlab [duplicate]

This question already has an answer here:
MATLAB one liner for batch assignment in 2D matrix?
(1 answer)
Closed 7 years ago.
So I'm a beginner in MATLAB so this question might be trivial.
Suppose x=[1 2 3 4 5] and y=[3 4 2 5 1] and img = zeros(5,5). I want to set img(1,3),(2,4),(3,2),(4,5),(5,1) to 1. How do I do this? When I simply try img(x,y), it takes all the combinations of indices like (1,3),(1,4),(1,2) etc. which is not what I want.
As you have experienced, MATLABs indexing doesn't work that way. To get a feeling for the ways indexing works in MATLAB, please have a look at this nice article from Mathworks.
Now how to tackle your problem: The solution is to use linear indexing. You can always index a 2-dimensional matrix either by (i,j) or with a linear index k which increases column-wise. You can convert between matrix-indexes and linear indexes using the sub2ind function. To get the correct indexes for your questions, use
img = zeros(5,5)
x = [1 2 3 4 5];
y = [3 4 2 5 1];
ind = sub2ind(size(img),x,y);
img(ind) = 1;

Convert a specific 2d array into 3d array

I want to convert a 2D matrix like A into a 3D matrix. Every slice should be the same content like this:
A=[1 2 3;4 5 6;7 8 9];
for i=1:10
B(:,:,i)=A
end
I need the same code without a loop, which decrease the speed of the program. In the original code A and i are rather big.
You can also try
B = A(:,:, ones(1,10) );
Running a small benchmark on ideone shows this approach significantly faster than bsxfun or repmat
I think the simplest and fastest way is using bsxfun, making use of the fact that it expands singleton dimensions:
A=[1 2 3;4 5 6;7 8 9];
B = bsxfun(#times,A,ones(3,3,10))
Here A is seen as a 3 x 3 x 1 matrix, and the third dimension is expanded to match the corresponding dimension in B (ie. 10).

quickly find the indexes of common rows in two matrices

Thanks in advance for the help.
Suppose that I have two matrices: A and B. I want to know which rows in A are also in B. For example given
A = [1 2; 3 4] and B = [1 2; 5 6; 7 8]
I would like an output of
out = [1 0];
A simple way of doing this is to use for loops but my A and B matrices are both very large. Using for loops is thus exceedingly slow (it would likely take several hours to handle just two matrices and I have several thousand to compare). Is there a way that I could do this using Matlab's built-in functions (which are optimized to handle matrix operations)?
There is a way to do it with MATLAB's built-in functions!
out = ismember(A, B, 'rows');

Access Elements of Array in MATLAB

I am trying to access some elements of an array in matlab. Consider the scenario below :
a = [1 2 3;4 5 6;7 8 9]
b = [1 2;2 1]
I want to access elements with indices (1,2) and (2,1) from a. I tried using a(b) etc. But none of the methods I tried worked.
How can this be done in matlab without using loops?
Also it would be helpful if you could suggest some good books for such basics in matlab.
First, convert your subscripts to indices using sub2ind:
dim1sub = b(:,1);
dim2sub = b(:,2);
ind = sub2ind(size(a), dim1sub, dim2sub)
After you have the indices
a(ind)
will give you:
ans =
2
4
See here for more information on matrix indexing.
Matlab lets you access a matrix with a linear index that scans through all the columns of the matrix. So in your case (with a 3x3) a(2,1)=a(2) and a(1,2)=a(4). The answer that #HebeleHododo provided takes your row and column index and converts them into a linear index into matrix a. Just remember that if you want to index a different size matrix, you will need a different linear index for it.
Also, there is a lot of information available online to help with learning matlab at http://www.mathworks.com/help/matlab/index.html#language-fundamentals or you can type doc help into the command window