I wanna load just one element from array using vld1q_s32, using Neon.
To be specific, if arr1 = [1, 2, 3, 4], I want to load each element to int32x4_t A0,
not in same order.
I wanna make A0 to [1, 1, 3, 3].
So I used
int32x4_t A0;
A0 = vld1q_s32(arr1[0]);
A0+1 = vld1q_s32(arr1[0]);
A0+2 = vld1q_s32(arr1[2]);
A0+3 = vld1q_s32(arr1[2]);
But it did not work.
How can I fix it?
Not using vld1q_s32 is O.K.
You can just load the data as-is then transpose the vector with itself:
A0 = vld1q_s32(arr1);
A0 = vtrn1q_s32(A0, A0);
Related
I have a MATLAB array and want to make a repetition based on the number of array elements. Below is the example that I want.
a = [2, 4, 6, 8]
If I want 7 elements, the result is
aa = [2, 4, 6, 8, 2, 4, 6]
Or if I want 5 elements,
aa = [2, 4, 6, 8, 2]
Is there any MATLAB function which makes these kind of result?
You can use "modular indexing":
a = [2, 4, 6, 8]; % data vector
n = 7; % desired number of elements
aa = a(mod(0:n-1, numel(a))+1);
One simple option will be to use a temporary variable for that:
a = [2 4 6 8];
k = 7;
tmp = repmat(a,1,ceil(k/numel(a)));
aa = tmp(1:k)
First, you repeat the vector using the smallest integer that makes the result larger than k, and then you remove the excess elements.
If you do that many times you can write a small helper function to do that:
function out = semi_repmat(arr,k)
tmp = repmat(arr,1,ceil(k/numel(arr)));
out = tmp(1:k);
end
Assume that we are given two vectors:
A=(a₁,a₂,...,aₘ)and B=(b₁,b₂,...,bₘ)
and we need to do something for all the vectors between these two ones.
For example, for A=(1,1,0)and B=(1,2,2), all the vectors between A and B are: {(1,1,1),(1,1,2),(1,2,0),(1,2,1)}.
An obvious way to generate such vectors is using m loops (for loop), but probably it is not the best one. I would like to know if someone has some better idea.
Here's a fixed method. Returns a matrix where each row is one of the vectors of the result.
% Data
A = [0, 0, 1, 3, 5, 2]
B = [4, 8, 5, 7, 9, 6]
% Preallocate
b = cell(1,numel(A));
vec = cell(1,numel(A));
% Make a vector of values of each element of the result
for i = 1:numel(A)
vec{i} = A(i):B(i);
end
% Get all combinations using ndgrid
[b{:}] = ndgrid(vec{:});
b=cat(ndims(b{1})+1,b{:});
% Reshape the numel(A)+1 dimensional array into a 2D array
res = reshape(b,numel(b)/length(A),length(A));
i am trying to swap 2 row vectors which are inside a row vector.
For example:
a=[1 2 3];
b=[5 3];
c=[9 3 7 6];
d=[7 5];
X1= [ a, b , d, c ];
I want to do random swapping such that two of the a,b,c,d remains at the same position in X1 and the remaining two of them shuffles in X1. For example, some of the possible random swaps are:
[b,a,d,c] % a and b swap with each other whereas d and c remain at the same place
[d,b,a,c] % a and d swap with each other whereas b and c remain at the same place
[c,b,d,a] % a and c swap with each other whereas b and d remain at the same place
.....
.....
The proper and safe way to what you're trying to do is by assigning your variables to a cell, permuting the elements of the cell, and finally concatenating the result.
Imagine a specific permutation, say, [c, b, a, d]. This permutation can be coded as [3, 2, 1, 4] in terms of a mapping. The corresponding code to generate your array is then:
% generate input
a = [1, 2, 3];
b = [5, 3];
c = [9, 3, 7, 6];
d = [7, 5];
% generate cell to permute
tmpcell = {a, b, c, d};
% define our permutation
permnow = [3, 2, 1, 4];
% permute and concatenate the result into an array
result = [tmpcell{permnow}];
% check if this is indeed OK:
disp(isequal(result,[c, b, a, d])) % should print 1
The only thing you might still need is to generate a random configuration. This is easy: you just have to choose 2 random indices and swap them in [1, 2, 3, 4]. A lazy option to do this:
nvars = length(tmpcell); % generalizes to multiple variables this way
idperm = 1:nvars;
i1 = randi(nvars,1);
partperm = setdiff(idperm, i1); % vector of remaining indices, avoid duplication
i2 = partperm(randi(nvars-1,1)); % second index, guaranteed distinct from i1
permnow = idperm;
permnow([i1, i2]) = [i2, i1]; % swap the two indices
I am trying to efficiently index a 2D array in Python and have the problem that it is really slow.
This is what I tried (simplified example):
xSize = veryBigNumber
ySize = veryBigNumber
a = np.ones((xSize,ySize))
N = veryBigNumber
const = 1
for t in range(N):
for i in range(xSize):
for j in range(ySize):
a[i,j] *= f(i,j)*const # f(i,j) is an arbitrary function of i and j.
Now I would like to substitute the nested loop by something more efficient. How do I do this?
Your 2D array could be produced using the following addition:
np.arange(200)[:,np.newaxis] + np.arange(200)
This type of vectorised operation is likely to be very fast:
>>> %timeit np.arange(200)[:,np.newaxis] + np.arange(200)
1000 loops, best of 3: 178 µs per loop
This method in not limited to addition. We can use the two arrays in the above operation as the arguments of any universal function (commonly abbreviated to ufunc).
For example:
>>> np.multiply(np.arange(5)[:,np.newaxis], np.arange(5))
array([[ 0, 0, 0, 0, 0],
[ 0, 1, 2, 3, 4],
[ 0, 2, 4, 6, 8],
[ 0, 3, 6, 9, 12],
[ 0, 4, 8, 12, 16]])
NumPy has built in ufuncs for all the basic arithmetic operations and some more interesting ones too. If you need a more exotic function, NumPy allows you to make your own ufunc.
Edit: To quickly explain the broadcasting happening in this method; you can think of it like this...
np.arange(5) produces 1D array which looks like this:
array([0, 1, 2, 3, 4])
The code np.arange(5)[:,np.newaxis] adds a second dimension (columns) to the range, producing this 2D array:
array([[0],
[1],
[2],
[3],
[4]])
To create the final 5x5 array using np.multiply (although we could use any ufunc or binary arithmetic operation), NumPy takes the 0 in the second array and mutliplies it with each elements it the first array making a row like this:
[ 0, 0, 0, 0, 0]
It then takes the second element in the second array, 1, and multiplies it with the first array, producing this row:
[ 0, 1, 2, 3, 4]
This continues until we have the final 5x5 matrix.
You could use the indices routine:
b=np.indices(a.shape)
a=b[0]+b[1]
Timings:
%%timeit
...: b=np.indices(a.shape)
...: c=b[0]+b[1]
1000 loops, best of 3: 370 µs per loop
%%timeit
for i in range(200):
for j in range(200):
a[i,j] = i + j
100 loops, best of 3: 10.4 ms per loop
Since your output matrix a is the element-wise power of N of a matrix F with elements f_ij = f(i,j) * const your code can simplify to
F = np.empty((xSize, ySize))
for i in range(xSize):
for j in range(ySize):
F[i,j] = f(i,j) * const
a = F ** n
For even more speed you can exchange the creation of the F matrix with something more efficient, given that the function f(i,j) is vectorized:
xmap, ymap = numpy.meshgrid(range(xSize), range(ySize))
F = f(xmap, ymap) * const
I am having trouble finding a matlab function to slice an element out of an array.
For example:
A = [1, 2, 3, 4]
I want to take out on element of this array, say the element 3:
B = [1, 2, 4]
Is there a matlab function for this or would I have to code the algorithm to construct a new array with all the elements of A except 3?
Do this:
index_of_element_to_remove = 3;
A(index_of_element_to_remove) = [];
now A will be [1 2 4]
If you want to remove more elements at the same time you can do:
index_of_element_to_remove = [1 3];
A(index_of_element_to_remove) = [];
now A will be [2 4]
By value, which will remove all elements equal to 3
A(find(A==3)) = []
Or by index
A(3) = []