Selecting Matrix element using vector elements [duplicate] - matlab

This question already has answers here:
Use a vector as an index to a matrix
(3 answers)
Closed 4 years ago.
Is there a way in Matlab to select a matrix element based off the elements in a vector? I don't think my description is clear, but what I effectively want to do is something similar to:
A=zeros(3,3,3) %3d matrix
A(1,1,2)=5
b=[1,1,2]
A(b)=5
Meaning, some easy way to select one element from a matrix using the entries in a vector as arguments. This exact example does not work because the last line counts b as a single argument, not three. I could write A(b(1),b(2),b(3)) but what I'm really looking for here is if there's a nice way of doing.

Method 1: Use sub2ind to find the linear index
You can define a function called findLinearIndex such that it convert the vector elements to the linear index of A:
findLinearIndex = #(A,b) sub2ind(size(A), b(1), b(2), b(3))
A(findLinearIndex(A,b)) = 5
Method 2: Convert the vector to cell array by num2cell
Then, you can use {:} to get the index
b_cell = num2cell(b) ;
A(b_cell{:}) = 5

Related

MATLAB - Repeat row n times (from A=[1;2;3] create B=[1;2;3;1;2;3;1;2;3]) [duplicate]

This question already has answers here:
Building a matrix by merging the same row vector multiple times
(2 answers)
Closed 5 years ago.
I would like to create, from the column matrix A=[1;2;3], another column matrix that repeats A n times. For example, being n=3, the new matrix would be B=[1;2;3;1;2;3;1;2;3]. Is there a way to to that (preferably without using loops)?
Thank you.
You can use repmat it is a fantastic function:
repmat(A,[n,1])
The first value of the second parameter is repetitions in the first dimension (columns), the second in the second dimension (rows) etc.
Another way to do it:
A2=A(:,ones(1,n));
B=A2(:)
Another way is to do using padarray.
a = [1 2 3]
b = padarray(a, [2 0], 'post', 'circular')
post means add to the end of the array, circular pads with circular repetition of elements.

What is the use of colon in function or variable in matlab.? [duplicate]

This question already has answers here:
The meaning of colon operator in MATLAB
(2 answers)
Closed 6 years ago.
% Select the region of interest from the original video
OutIm = Im(roi(2):end, roi(1):end, :);
YCbCr = step(hColorConv, OutIm);
CbCr = complex(YCbCr(:,:,2), YCbCr(:,:,3));
What is use of : in the above code?
From the documentation; it means 'all the elements' of the column or the row. More information in the slicing section of the documentation.
So basically, assuming a is a 2-dimensional array, a(:,1) refers to all the elements of the first column, while a(2,:) refers to all the elements of the second line..
In your case, YCbCr is a 3-dimensional array, and YCbCr(:,:,2) to matrix of the second elements (Cb) and YCbCr(:,:,3) to the third element (Cr).

What is the meaning of [] in a matlab function argument? [duplicate]

This question already has an answer here:
How to use reshape in Matlab?
(1 answer)
Closed 7 years ago.
I found the following function call:
reshape(A, 1, [])
This flattens matrix A colum major. I am trying to understand the call. The function documentation says after A there should be a size vector for the reshaped matrix, but in here there is a one followed by [] instead of a two-vector. Is this a way of saying "Do whatever it takes so the matrix will have one row, I don't care what the width is"?
How come Matlab lets you exchange one argument for two like this? I tried googling around and did not find an explanation, and I want to understand what's going on here.
[] is an empty matrix. In many MATLAB built-in functions, an empty matrix is interpreted to mean "use the default argument here" or "automatically determine this value". Occasionally it is used to disambiguate two meanings of a function, as with the max function, where max(A,2) compares each element of A to 2 and returns the larger, while max(A,[],2) finds the largest element of each row.
If you read the help for reshape, you will see the following:
You can specify a single dimension size of [] to have the dimension size automatically calculated, such that the number of elements in B matches the number of elements in A. For example, if A is a 10-by-10 matrix, then reshape(A,2,2,[]) reshapes the 100 elements of A into a 2-by-2-by-25 array.

How to Access the Cell array Matrix? [duplicate]

This question already has answers here:
How to use cell arrays in Matlab?
(2 answers)
Closed 8 years ago.
Could any one tell me about the indexing of a cell Array? I've tried to google it but I could only find unsatisfied result (may be I'm not good in googling). For matrix indexing I found a good document which can be found here. For my case let take a simple example.
a = {ones(10)}
and I want to access the first element of a. Something like
a(1,1) % this will give a 10 x 10 matrix but i am not looking for it.
I can do it by changing it into a matrix like
a = cell2mat(a);
a(1,1)
ans = 1
but is there any direct way to access first element of cell array sub matrix.
To access the first element of a the first cell in a cell array, you may do:
a = {ones(10)};
a{1}(1)
If you have multidimensional cell arrays, with multidimensional numerical arrays inside it, you can do:
a{2,3}(4,5)
This will give you element (4,5) of cell (2,3).
You actually are accessing the first element of a, and it contains a matrix of 10×10 filled with ones.
Initializing a cell can be done by a = cell(10), and obtaining a certain value of the cell matrix is done with a{i,j}. See also the documentation.
For initializing a cell array with some values, see this question.

Matlab: assign to matrix with column\row index pairs [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I change the values of multiple points in a matrix?
I have a matrix A and three vectors of the same length, r, holding the indexes of the rows to assign to, c, holding the indexes of the columns to assign to, and v containing the actual values to assign.
What I want to get is A(r(i),c(i))==v(i) for all i. But doing
A(r,c)=v;
Doesn't yield the correct result as matlab interprets it as choosing every possible combination of r and c and assigning values to it, for instance
n=5;
A=zeros(n);
r=1:n;
c=1:n;
A(r,c)=1;
Yields a matrix of ones, where I would like to get the identity matrix since I want A(r(i),c(i))==1 for each i, that is only elements on the diagonal should be affected.
How can I achieve the desired result, without a for loop?
OK, I've found the answer - one needs to use linear indexing, that is convert the column\row pairs into a single index:
idx = sub2ind(size(A), r,c);
A(idx)=v;