vector of indices - matlab

I have a 10x10 matrix called A:
I have vector of column numbers:
C = [ 2, 6, 8 ];
I have a vector of row numbers:
R = [1; 3; 7];
The column numbers correspond to each row. i.e. For column 1 we are looking at row numbers given by R, for column 3 we are looking at row numbers given by R and so on.
I want to replace those exact locations in A with some other number 13.
i.e. for each of these locations in matrix A:
(1,2) (1,6) (1,8), (3,2), (3, 6), (3,8) I want to insert 13.
How do I achieve the above ?

you can do A(R,C) = 13 .......

As dlavila pointed out, you can do A(R,C) = 13 wich would be the best and easiest. Nevertheless I have written a longer code involving the eval function that you might find useful in the future:
for ii=1:length(C)
for jj =1:length(R)
eval(strcat('A(', num2str(C(ii)), ',',num2str(R(jj)),')=13;'))
end
end
Both give the same results.

Related

I need help to understand the functions of Matlab [duplicate]

This question already has answers here:
MATLAB - extracting rows of a matrix
(5 answers)
Closed 7 years ago.
Can someone explain to me the solution of this exercise:
Exercise
Write a function called odd_index That Takes a matrix, M, as input argument and returns a matrix That contains only elements of M Those That are in odd rows and columns. In other words, it would return the elements of M at indices (1,1), (1,3), (1,5), ..., (3,1), (3,3), (3,5) , …, etc. That note both the row and the column of an element must be odd to be included in the output. The following would not be returned: (1,2), (2,1), (2,2) Because Either the row or the column or both are even. As an example, if M Were a 5-by-8 matrix, the output must be then a 3-by-4 Because the function omits rows 2 and 4 of M and it omits Also columns 2, 4, 6, and 8 of M .
Solution:
 M_out = odd_index function (M)
 M_out = M (1: 2: end, 1: 2: end);
end
Link solution: Return only odd elements
Can someone explain to me how they came to the function M_out = M (1: 2: end, 1: 2: end) ;.
function M_out = odd_index (M)
M_out = M(1: 2: end, 1: 2: end);
end
I takes each odd element of the matrix M and returns that to a matrix M_out,
M = [1 2 3; 4 5 6;7 8 9];
M_out = odd_index(M)
M_out =
1 3
7 9
where you can see that elements on an odd row and on an odd column are being printed, but the elements on even rows and even columns are being left out.
The trick here is to step through the indices in steps of 2 instead of 1. M(1,1) gives the element in the upper-left corner of M, i.e. 1. M(1:2,1) returns the first two elements in the left column: 1 and 4. This happens because n:m creates a vector of numbers running from n to m in steps of 1. You can change this step size by adding a number: n:x:m, where x specifies how large your steps are. Since the odd numbers are 2 apart, just start your vector at the lowest, positive, odd number, 1, and step with size 2. The last element in your vector 1:2:end simply means "the end of the vector", thus, if M = [5x3] the end of the rows is 5, because there are five rows, and the end of the columns would be 3, since there are three columns.

MATLAB: scan the row of a matrix and use values to search another matrix

I have matrix X and A where
X = [x1, y1, 1, 1; x2, y2, 1, 3; x3, y, 2, 4]
A = [1, 1, 0; 1, 3, 1; 1, 4, 2]
I want to:
1. scan the last two columns for every row in X (FYI, these two number combinations are unique)
2. find those values in the first two columns of A
3. get the value of the last column in that row of A.
For example, for the first row of X, I get 1 and 1, so I find 1 and 1 for the first two columns in A (which appears to be the first row), so the number I want to get is 0.
I think I can do it using a loop and a "find" function if it were just one number I'm working with, but I'm new to matlab and have trouble with a combination of two numbers. I would appreciate your help!
The ismember function may be what you're looking for, along with the () and : operators to extract columns from an array.
% Map rows in X to rows in A
[tf,loc] = ismember(X(:,[3 4]), A(:,[1 2]), 'rows');
% Grab the corresponding value from A
rslt = A(loc, 3);
Now you have a logical vector tf that indicates for each row in X whether it was found in A, and loc, which holds the corresponding indexes in to the rows of A for the ones that matched. Then you use those indexes to index in to A to pull out the "value" or dependent variable columns. These are vectorized operations, so it'll be faster than doing it with loops and find().
Read through the documentation for ismember, unique, paren, and the functions they reference to get more background on Matlab's functions for doing recordwise searching like this.

Get even/odd indices of a matrix - MATLAB

I have following problem:
I have a given matrix of let's say 4x4.
How can I get the indices of the following combinations:
row odd and column odd
row odd and column even
row even and column odd
row even and column even
For example if I have the matrix:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
'row odd and column odd' would be the indices of 1, 3, 9, 11...
'row odd and column even' would be the indices of 2, 4, 10, 12...
'row even and column odd' would be the indices of 5, 7, 13, 15...
and 'row even and column even' would be indices of 6, 8, 14, 16...
Also, is it possible to combine those operations so e.g. I get the indices for 'row odd and column odd' and 'row even and column even'?
Thank you!
It's pretty easy to do with indexing:
Odd rows and odd columns
B = A(1:2:end, 1:2:end);
Odd rows and even columns
B = A(1:2:end, 2:2:end);
Even rows and odd columns
B = A(2:2:end, 1:2:end);
Even rows and even columns
B = A(2:2:end, 2:2:end);
The above assumes that you want the actual matrix values themselves. It's a bit confusing as your matrix elements are the same as linear indexing values themselves. If you want to determine the actual column major indices to access the matrix, you can generate a vector from 1 to N where N is the total number of elements in your matrix, then reshape this matrix into the desired size that you want. After, use the same logic above to get the actual linear indices:
N = numel(A);
B = reshape(1:N, size(A,1), size(A,2));
ind = B(1:2:end, 1:2:end); %// For odd rows, odd columns
%// Repeat for the other ones...
Now, given your comment, you want to create a new matrix that will store only these extracted matrix values while making all of the other elements zero. If you want to do this, simply pre-allocate a matrix of zeroes, then copy over those values to extract using the computed indices into the new matrix. In other words:
N = numel(A);
B = reshape(1:N, size(A,1), size(A,2));
ind = B(1:2:end, 1:2:end); %// For odd rows, odd columns - Change to suit your tastes
out = zeros(size(A));
out(ind(:)) = A(ind(:));
If you want to combine the indices like having odd row - odd column, and even row - even column, just compute two sets of indices, concatenate them into a single vector and do the same syntax like before. Therefore:
N = numel(A);
B = reshape(1:N, size(A,1), size(A,2));
ind = B(1:2:end, 1:2:end); %// For odd rows, odd columns
ind2 = B(2:2:end, 2:2:end); %// For even rows, even columns
ind = [ind(:); ind2(:)];
out = zeros(size(A));
out(ind) = A(ind);
Code
N = size(A,1); %// Get size of input matrix A
case1_ind = bsxfun(#plus,[1:2:N]',(0:N/2-1)*2*N)
case2_ind = case1_ind + N
case3_ind = case1_ind + 1
case4_ind = case3_ind + N
Note: These outputs are indices. So, to get the actual outputs, use these as indices.
To combine indices for case 1 and case 4, just concatenate -
case14comb_ind = [case1_ind ; case4_ind]
Edit :
%// To copy onto some other matrix of the same size as A, do this for case 1
new_matrix = zeros(size(A))
new_matrix(case1_ind(:)) = A(case1_ind(:))
Repeat this for the other cases too.

Choose multiple values from multiple rows and columns from matrix in matlab

Say I have a matrix
A = [1, 2, 3; 4 5 6; 7 8 9]
If I want to choose say (1,2), (2,3)
I could not say A(1:2,2:3) otherwise it will return a 2*2 matrix, what should I do it for only one time...
Use sub2ind to linearly index (to avoid creating a matrix) using the given row and column numbers -
ind = [1,2;2,3]
A(sub2ind(size(A),ind(:,1),ind(:,2)))

How to find all index pairs of unequal elements in vector (Matlab)

Lets say I have the following vector in Matlab:
V = [4, 5, 5, 7];
How can I list (in a n-by-2 matrix for example) all the index pairs corresponding to unequal elements in the vector. For example for this particular vector the index pairs would be:
index pairs (1, 2) and (1, 3) corresponding to element pair (4,5)
index pair (1, 4) corresponding to element pair (4,7)
index pairs (2, 4) and (3, 4) corresponding to element pair (5,7)
The reason I need this is because I have a cost-function which takes a vector such as V as input and produces a cost-value.
I want to see how does the random swapping of two differing elements in the vector affect the cost value (using this for steepest descent hill climbing).
The order of the index pairs doesn't also matter. For my purposes (1,2) is the same as (2,1).
For example if my cost-function was evalCost(), then I could have V = [4, 5, 5, 7] and
evalCost(V) = 14
whereas for W = [4, 7, 5, 5] the cost could be:
evalCost(W) = 10
How to get the list of "swapping" pair indexes in Matlab. Hope my question is clear =)
I don't understand the cost function part, but the first part is simple:
[a,b]=unique(V)
C = combnk(b,2)
C contains the indices, and V(C) the values:
C = combnk(b,2)
C =
1 2
1 4
2 4
V(C)
ans =
4 5
4 7
5 7
Use bsxfun and then the two-ouput version of find to get the pairs. triu is applied to the output of bsxfun to consider only one of the two possible orders.
[ii jj] = find(triu(bsxfun(#ne, V, V.')));
pairs = [ii jj];