Get the values of an array from indices in another array: Matlab - matlab

I have a 20x1 double array A, and a 1000x1 double array B.
I want to get array C, where array C will be 1000x1 double where the values in B are used to index the values in A like so:
C(1) = A(B(1))
C(2) = A(B(2))
...
C(i) = A(B(i))
...
c(1000) = A(B(1000))
How this can be done?

You don't need a loop for this, you can directly use:
C = A(B)
This takes advantage of MATLAB's matrix indexing, which is the way indexing is handled in MATLAB when an array is used instead of an integer.
Take a look at the docs: https://uk.mathworks.com/help/matlab/math/matrix-indexing.html
For example:
A = [11 12 13];
B = [1 2 3 1 2 3 3 2 1];
C = A(B)
C =
11 12 13 11 12 13 13 12 11
Ensure that B only contains integers which are valid indices of A (not less than 1 or greater than the length of A).

I did it using for loop as shown below, not sure if this is the ideal solution:
C = zeros(1000,1);
for i = 1:1000
C(i,1) = A(B(i));
end

Related

How to get all the possible combinations of elements in a matrix, but don't allow exchange of elements inbetween columns?

Lets say I have this matrice A: [3 x 4]
1 4 7 10
2 5 8 11
3 6 9 12
I want to permute the element of in each column, but they can't change to a different column, so 1 2 3 need to always be part of the first column. So for exemple I want:
3 4 8 10
1 5 7 11
2 6 9 12
3 4 8 11
1 6 7 10
2 5 9 12
1 6 9 11
. . . .
So in one matrix I would like to have all the possible permutation, in this case, there are 3 different choices 3x3x3x3=81possibilities.So my result matrixe should be 81x4, because I only need each time one [1x4]line vector answer, and that 81 time.
An other way to as the question would be (for the same end for me), would be, if I have 4 column vector:
a=[1;2;3]
b=[4;5;6]
c=[7;8;9]
d=[10;11;12;13]
Compare to my previous exemple, each column vector can have a different number of row. Then is like I have 4 boxes, A, B C, D and I can only put one element of a in A, b in B and so on; so I would like to get all the permutation possible with the answer [A B C D] beeing a [1x4] row, and in this case, I would have 3x3x3x4=108 different row. So where I have been missunderstood (my fault), is that I don't want all the different [3x4] matrix answers but just [1x4]lines.
so in this case the answer would be:
1 4 7 10
and 1 4 7 11
and 1 4 7 12
and 1 4 7 13
and 2 4 8 10
and ...
until there are the 108 combinations
The fonction perms in Matlab can't do that since I don't want to permute all the matrix (and btw, this is already a too big matrix to do so).
So do you have any idea how I could do this or is there is a fonction which can do that? I, off course, also could have matrix which have different size. Thank you
Basically you want to get all combinations of 4x the permutations of 1:3.
You could generate these with combvec from the Neural Networks Toolbox (like #brainkz did), or with permn from the File Exchange.
After that it's a matter of managing indices, applying sub2ind (with the correct column index) and rearranging until everything is in the order you want.
a = [1 4 7 10
2 5 8 11
3 6 9 12];
siz = size(a);
perm1 = perms(1:siz(1));
Nperm1 = size(perm1,1); % = factorial(siz(1))
perm2 = permn(1:Nperm1, siz(2) );
Nperm2 = size(perm2,1);
permidx = reshape(perm1(perm2,:)', [Nperm2 siz(1), siz(2)]); % reshape unnecessary, easier for debugging
col_base_idx = 1:siz(2);
col_idx = col_base_idx(ones(Nperm2*siz(1) ,1),:);
lin_idx = reshape(sub2ind(size(a), permidx(:), col_idx(:)), [Nperm2*siz(1) siz(2)]);
result = a(lin_idx);
This avoids any loops or cell concatenation and uses straigh indexing instead.
Permutations per column, unique rows
Same method:
siz = size(a);
permidx = permn(1:siz(1), siz(2) );
Npermidx = size(permidx, 1);
col_base_idx = 1:siz(2);
col_idx = col_base_idx(ones(Npermidx, 1),:);
lin_idx = reshape(sub2ind(size(a), permidx(:), col_idx(:)), [Npermidx siz(2)]);
result = a(lin_idx);
Your question appeared to be a very interesting brain-teaser. I suggest the following:
in = [1,2,3;4,5,6;7,8,9;10,11,12]';
b = perms(1:3);
a = 1:size(b,1);
c = combvec(a,a,a,a);
for k = 1:length(c(1,:))
out{k} = [in(b(c(1,k),:),1),in(b(c(2,k),:),2),in(b(c(3,k),:),3),in(b(c(4,k),:),4)];
end
%and if you want your result as an ordinary array:
out = vertcat(out{:});
b is a 6x3 array that contains all possible permutations of [1,2,3]. c is 4x1296 array that contains all possible combinations of elements in a = 1:6. In the for loop we use number from 1 to 6 to get the permutation in b, and that permutation is used as indices to the column.
Hope that helps
this is another octave friendly solution:
function result = Tuples(A)
[P,n]= size(A);
M = reshape(repmat(1:P, 1, P ^(n-1)), repmat(P, 1, n));
result = zeros(P^ n, n);
for i = 1:n
result(:, i) = A(reshape(permute(M, circshift((1:n)', i)), P ^ n, 1), i);
end
end
%%%example
A = [...
1 4 7 10;...
2 5 8 11;...
3 6 9 12];
result = Tuples(A)
Update:
Question updated that: given n vectors of different length generates a list of all possible tuples whose ith element is from vector i:
function result = Tuples( A)
if exist('repelem') ==0
repelem = #(v,n) repelems(v,[1:numel(v);n]);
end
n = numel(A);
siz = [ cell2mat(cellfun(#numel, A , 'UniformOutput', false))];
tot_prd = prod(siz);
cum_prd=cumprod(siz);
tot_cum = tot_prd ./ cum_prd;
cum_siz = cum_prd ./ siz;
result = zeros(tot_prd, n);
for i = 1: n
result(:, i) = repmat(repelem(A{i},repmat(tot_cum(i),1,siz(i))) ,1,cum_siz(i));
end
end
%%%%example
a = {...
[1;2;3],...
[4;5;6],...
[7;8;9],...
[10;11;12;13]...
};
result =Tuples(a)
This is a little complicated but it works without the need for any additional toolboxes:
You basically want a b element 'truth table' which you can generate like this (adapted from here) if you were applying it to each element:
[b, n] = size(A)
truthtable = dec2base(0:power(b,n)-1, b) - '0'
Now you need to convert the truth table to linear indexes by adding the column number times the total number of rows:
idx = bsxfun(#plus, b*(0:n-1)+1, truthtable)
now you instead of applying this truth table to each element you actually want to apply it to each permutation. There are 6 permutations so b becomes 6. The trick is to then create a 6-by-1 cell array where each element has a distinct permutation of [1,2,3] and then apply the truth table idea to that:
[m,n] = size(A);
b = factorial(m);
permutations = reshape(perms(1:m)',[],1);
permCell = mat2cell(permutations,ones(b,1)*m,1);
truthtable = dec2base(0:power(b,n)-1, b) - '0';
expandedTT = cell2mat(permCell(truthtable + 1));
idx = bsxfun(#plus, m*(0:n-1), expandedTT);
A(idx)
Another answer. Rather specific just to demonstrate the concept, but can easily be adapted.
A = [1,4,7,10;2,5,8,11;3,6,9,12];
P = perms(1:3)'
[X,Y,Z,W] = ndgrid(1:6,1:6,1:6,1:6);
You now have 1296 permutations. If you wanted to access, say, the 400th one:
Permutation_within_column = [P(:,X(400)), P(:,Y(400)), P(:,Z(400)), P(:,W(400))];
ColumnOffset = repmat([0:3]*3,[3,1])
My_permutation = Permutation_within_column + ColumnOffset; % results in valid linear indices
A(My_permutation)
This approach allows you to obtain the 400th permutation on demand; if you prefer to have all possible permutations concatenated in the 3rd dimension, (i.e. a 3x4x1296 matrix), you can either do this with a for loop, or simply adapt the above and vectorise; for example, if you wanted to create a 3x4x2 matrix holding the first two permutations along the 3rd dimension:
Permutations_within_columns = reshape(P(:,X(1:2)),3,1,[]);
Permutations_within_columns = cat(2, Permutations_within_columns, reshape(P(:,Y(1:2)),3,1,[]));
Permutations_within_columns = cat(2, Permutations_within_columns, reshape(P(:,Z(1:2)),3,1,[]));
Permutations_within_columns = cat(2, Permutations_within_columns, reshape(P(:,W(1:2)),3,1,[]));
ColumnOffsets = repmat([0:3]*3,[3,1,2]);
My_permutations = Permutations_within_columns + ColumnOffsets;
A(My_permutations)
This approach enables you to collect a specific subrange, which may be useful if available memory is a concern (i.e. for larger matrices) and you'd prefer to perform your operations by blocks. If memory isn't a concern you can get all 1296 permutations at once in one giant matrix if you wish; just adapt as appropriate (e.g. replicate ColumnOffsets the right number of times in the 3rd dimension)

Store vectors from structure to matrix without a for loop?

My issue is fairly simple. I have vectors of the same size in a struct (let's call it a.f). Let's say we have 8 structs (8 <1x1 struct>) each containing another structure b which has a vector in it. I'm aware that I can do either this:
for i = 1:8
matrix(i,:) = a(i).f(1:5)
end
or
for i = 1:8
matrix{i} = a(i).f(1:5)
end
Is there another way to store all these arrays into a matrix, without the need for a for loop?
matrix = [a.f];
just stores them all as a vector.
Just use vertcat if your vectors are rows, or horzcat if they are columns.
Example:
>> a(1).f = 1:7;
>> a(2).f = 11:17;
>> matrix = vertcat(a.f)
matrix =
1 2 3 4 5 6 7
11 12 13 14 15 16 17
how about using reshape after [a.f] ? for example:
matrix =reshape([a.f],numel(a),[]);
How about
matrix = zeros(numel(a),numel(a(1).f));
matrix(:) = [a.f];

How to compare array to a number for if statement?

H0 is an array ([1:10]), and H is a single number (5).
How to compare every element in H0 with the single number H?
For example in
if H0>H
do something
else
do another thing
end
MATLAB always does the other thing.
if requires the following statement to evaluate to a scalar true/false. If the statement is an array, the behaviour is equivalent to wrapping it in all(..).
If your comparison results in a logical array, such as
H0 = 1:10;
H = 5;
test = H0>H;
you have two options to pass test through the if-statement:
(1) You can aggregate the output of test, for example you want the if-clause to be executed when any or all of the elements in test are true, e.g.
if any(test)
do something
end
(2) You iterate through the elements of test, and react accordingly
for ii = 1:length(test)
if test(ii)
do something
end
end
Note that it may be possible to vectorize this operation by using the logical vector test as index.
edit
If, as indicated in a comment, you want P(i)=H0(i)^3 if H0(i)<H, and otherwise P(i)=H0(i)^2, you simply write
P = H0 .^ (H0<H + 2)
#Jonas's nice answer at his last line motivated me to come up with a version using logical indexing.
Instead of
for i=1:N
if H0(i)>H
H0(i)=H0(i)^2;
else
H0(i)=H0(i)^3;
end
end
you can do this
P = zeros(size(H0)); % preallocate output
test = H0>H;
P(test) = H0(test).^2; % element-wise operations
% on the elements for which the test is true
P(~test) = H0(~test).^3; % element-wise operations
% on the elements for which the test is false
Note that this is a general solution.
Anyway take a look at this: using ismemeber() function. Frankly not sure how do you expect to compare. Either greater than, smaller , equal or within as a member. So my answer might not be yet satisfying to you. But just giving you an idea anyway.
H0 = [0 2 4 6 8 10 12 14 16 18 20];
H = [10];
ismember(H,H0)
IF (ans = 1) then
// true
else
//false
end
Update Answer
This is super bruteforce method - just use it explain. You are better off with any other answers given here than what I present. Ideally what you need is to rip off greater/lower values into two different vectors with ^3 processing - I assume... :)
H0 = [0 2 4 6 8 10 12 14 16 18 20];
H = [10];
H0(:)
ans =
0
2
4
6
8
10
12
14
16
18
20
Function find returns indices of all values in H0 greater than 10 values in a linear index.
X = find(H0>H)
X =
7
8
9
10
11
Function find returns indices of all values in H0 lower than 10 in a linear index.
Y = find(H0<H)
Y =
1
2
3
4
5
6
If you want you can access each element of H0 to check greater/lower values or you can use the above matrices with indices to rip the values off H0 into two different arrays with the arithmetic operations.
G = zeros(size(X)); // matrix with the size = number of values greater than H
J = zeros(size(Y)); // matrix with the size = number of values lower than H
for i = 1:numel(X)
G(i) = H0(X(i)).^3
end
G(:)
ans =
1728
2744
4096
5832
8000
for i = 1:numel(Y)
J(i) = H0(Y(i)).^2
end
J(:)
ans =
0
4
16
36
64
100

Extract every element except every n-th element of vector

Given a vector
A = [1,2,3,...,100]
I want to extract all elements, except every n-th. So, for n=5, my output should be
B = [1,2,3,4,6,7,8,9,11,...]
I know that you can access every n-th element by
A(5:5:end)
but I need something like the inverse command.
If this doesn't exist I would iterate over the elements and skip every n-th entry, but that would be the dirty way.
You can eliminate elements like this:
A = 1:100;
removalList = 1:5:100;
A(removalList) = [];
Use a mask. Let's say you have
A = 1 : 100;
Then
m = mod(0 : length(A) - 1, 5);
will be a vector of the same length as A containing the repeated sequence 0 1 2 3 4.
You want everything from A except the elements where m == 4, i.e.
B = A(m ~= 4);
will result in
B == [1 2 3 4 6 7 8 9 11 12 13 14 16 ...]
Or you can use logical indexing:
n = 5; % remove the fifth
idx = logical(zeroes(size(A))); % creates a blank mask
idx(n) = 1; % makes the nth element 1
A(idx) = []; % ta-da!
About the "inversion" command you cited, it is possible to achieve that behavior using logical indexing. You can negate the vector to transform every 1 in 0, and vice-versa.
So, this code will remove any BUT the fifth element:
negatedIdx = ~idx;
A(negatedIdx) = [];
why not use it like this?
say A is your vector
A = 1:100
n = 5
B = A([1:n-1,n+1:end])
then
B=[1 2 3 4 6 7 8 9 10 ...]
One possible solution for your problem is the function setdiff().
In your specific case, the solution would be:
lenA = length(A);
index = setdiff(1:lenA,n:n:lenA);
B = A(index)
If you do it all at once, you can avoid both extra variables:
B = A( setdiff(1:end,n:n:end) )
However, Logical Indexing is a faster option, as tested:
lenA = length(A);
index = true(1, lenA);
index(n:n:lenA) = false;
B = A(index)
All these codes assume that you have specified the variable n, and can adapt to a different value.
For the shortest amount of code, you were nearly there all ready. If you want to adjust your existing array use:
A(n:n:end)=[];
Or if you want a new array called B:
B=A;
B(n:n:end)=[];

"Desort" a vector (undo a sorting)

In Matlab, sort returns both the sorted vector and an index vector showing which vector element has been moved where:
[v, ix] = sort(u);
Here, v is a vector containing all the elements of u, but sorted. ix is a vector showing the original position of each element of v in u. Using Matlab's syntax, u(ix) == v.
My question: How do I obtain u from v and ix?
Of course, I could simply use:
w = zero(size(v));
for i = 1:length(v)
w(ix(i)) = v(i)
end
if nnz(w == u) == length(u)
print('Success!');
else
print('Failed!');
end
But I am having this tip-of-the-tongue feeling that there is a more elegant, single-statement, vectorized way of doing this.
If you are wondering why one would need to do this instead of just using u: I was trying to implement the Benjamini-Hochberg procedure which adjusts each element of the vector based on its position after sorting, but recovering the original order after adjusting was important for me.
The solution is:
w(ix) = v;
This is a valid Matlab operation provided that w is either at least as big as v, or not yet declared.
Example:
>> u = [4 8 10 6 2];
>> [v, ix] = sort(u)
v = 2 4 6 8 10
ix = 5 1 4 2 3
>> u(ix)
ans = 2 4 6 8 10
>> w(ix) = v
w = 4 8 10 6 2
(Apologies for the trivial question-answer, but I realized the solution as I was typing the question, and thought it might be useful to someone.)