How to find all index pairs of unequal elements in vector (Matlab) - 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];

Related

Is it possible to replace a value in a matrix with a list?

For example, suppose I have a matrix
5 6 7
1 2 3
and I want to change each element of the matrix so that they contain position information, and form a new matrix. Like
(5, 1, 1) (6, 1, 2) (7, 1, 3)
(1, 2, 1) (2, 2, 2) (3, 2, 3)
Any ideas?
You could make them as cell arrays.
If the original matrix is called dat,
dat = [5 6 7; 1 2 3];
for i = 1:size(dat,1)
for j = 1:size(dat,2)
newdat{i,j} = [dat(i,j),i,j]
end
end
Use additional dimension for list elements
If all elements have the same list length associated, what is always the case in your question, I would recommend the use of an additional dimension for your matrix to store the list elements.
% your data
dat = [5 6 7; 1 2 3];
% Size of your data
s = size(dat)
% Create a target array with an added dimension with 3 elements
% 1st is your original data
% 2nd is first position index
% 3rd is second position index
dat1 = zeros([s,3]);
% Create an array with subscripts
[i1,i2] = ind2sub(s,1:numel(dat));
% fill in original data
dat1(:,:,1) = dat;
% Assign subscripts to your array
dat1(:,:,2) = reshape(i1,s);
dat1(:,:,3) = reshape(i2,s);
% Display result
dat1
This solution has an efficient runtime and memory behaviour, is fully vectorized (no for loops) and can be scaled to more dimensions or with a little effort to n-dimensional functionality.
octave
matlab

How can I find each max element of three matrices as new matrix?

Maybe the question is a little bit confused, I'll make an example below.
Let's say I have a 3 matrices a, b, c with same size.
a = [2, 5; 6, 9];
b = [3, 3; 8, 1];
c = [5, 5; 2, 7];
How can I get the new matrix max with each max element in all three matrices?
max = [5, 5; 8, 9]
I know I could create logical matrix like a>b and then do the math, calc it out, is there any other more efficient way to do it?
You can concatenate the matrices into one 2x2x3 matrix using
d=cat(3,a,b,c)
and then use max-function to get your desired output:
maxValues=max(d,[],3)
The 3rd input to max defines along which dimension of the first input you want to find the maximum value.

vector of indices

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.

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

MATLAB addressing different matrix elments with an index?

How can I create an index-matrix that specifies which elements of a matrix to address?
So for example I have a matrix A which is 80 by 50. I know that A(1:5,:) addresses only the first 5 elements, but what if I want to multiply A with another matrix which also changes the elements to be addressed? So I want to multiply B(1,:) with A(1:5,:), and B(2,:) with A(10:15,:) and so on. Is there a smart way to specify this index-matrix where the information (1:5; 10:15, etc.) is stored?
Yes you can certainly define indices into a matrix using another matrix. Here is a simple example using a cell array to store the index list:
X =[1,2,3,4,5,6]
Idx = { [1, 2, 3], [4, 5, 6] }
Y = X( Idx{1} ) .* X( {Idx{2} )
Y = [ 4, 10, 18]