Is there a quick way concat the values returned by Julia's digits() method back into it's original number? [duplicate] - arrays

This question already has answers here:
Get a number from an array of digits
(3 answers)
Closed 3 years ago.
I really like Julia's Digits function which will return an array of the individual digits that make up the input integer (See code example). My question is, once you have this array, is there an easy way to join the individual values back into the original value passed into digits?
Now, I suppose I could just take each integer's index value and multiply by it's corresponding power of 10 and modify the array in place. Then I could use sum on the array, but is there a better way to do it?
I.E
function getDigits(x)
return digits(x)
end
Julia> getDigits(1234)
4-element Array{Int64,1}:
4
3
2
1
function joinDigits(digitArray)
for i in 0:length(digitArray)-1
digitArray[i+1] = digitArray[i+1] * 10 ^ i
end
return sum(digitArray)
Julia> joinDigits([4,3,2,1])
1234

foldr((rest, msd) -> rest + 10*msd,x) is a one liner that does the same thing. It's terse, but probably not as clean as the for loop.

Related

Python numpy: Selecting array entries based on input array [duplicate]

This question already has answers here:
Getting the indices of several elements in a NumPy array at once
(5 answers)
Closed 2 years ago.
Assume I have an array:
a = np.array([1,2,3,4,5])
Now I want to find the indices of elements in this array corresponding to the values given by another array input:
input = np.array([2,4,5])
The expected result should be:
result = [1,3,4]
A boolean mask, which is true for element indices 1,3,4 would also be fine.
I do not want to use looping to solve this. I assume that a possible solution has to do with the numpy where() function, but using this one, I am only able to compare the entries of array a with one element of array input at a time. Because the length of input might differ, I cannot really use this approach. Do you have any other ideas?
Thanks in advance.
np.where(np.in1d(a, inp))[0]
or:
np.isin(a, inp).nonzero()[0]
or as suggested here:
sorter = np.argsort(a)
sorter[np.searchsorted(a, inp, sorter=sorter)]
output:
[1 3 4]
np.where(np.in1d(a, inp))[0] np.where(np.in1d(a, inp))[0]

how to access an element of an n-D matrix where index comes from a mathematical operation [duplicate]

This question already has answers here:
MATLAB: Accessing an element of a multidimensional array with a list
(2 answers)
Use a vector as an index to a matrix
(3 answers)
Closed 5 years ago.
How can I access an element of an n-D matrix where index comes from a mathematical operation in Matlab?
For example I have a 4D Matrix called A.
I want to access element 1,1,1,1 which results from (3,4,5,6) - (2,3,4,5)
Is there any way I can do this assuming that the array can be any dimension d and that the array from subtraction will always be d elements long?
One possible way would be to utilise the fact that MATLAB can use linear indexing for any n-dimensional array as well as row-column type indexing. Then you just have to calculate the linear index of your operation result.
There may be a more elegant way to do this but if x is the array holding the result of your operation, then the following works
element = A(sum((x-1).*(size(A).^[0:length(size(A))-1]))+1);
The sub2ind function feels like it should help here, but doesn't seem to.
Another approach is to converting to a cell array, then to a comma-separated list:
A = rand(3,4,5,6); % example A
t = [2 1 3 4]; % example index
u = num2cell(t);
result = A(u{:});

Extract one dimension from a multidimensional array [duplicate]

This question already has answers here:
On shape-agnostic slicing of ndarrays
(2 answers)
Closed 6 years ago.
Suppose A is multi-dimensional array (MDA) of size 3,4,5 and B is another MDA of size 3,4,5,6.
I know A(1,:,:) or B(1,:,:,:) can both extract their elements along the first dimension.
I now need to write a general program to extract the k-th dimension from a MDA without knowing its size.
For example, the MDA C has 6 dimension: 4,5,6,7,8,9 and I want an extraction C(:,:,k,:,:,:).
Sometimes, the MDA 'D' has 4 dimension: 3,4,5,6 and I want another extraction D(k,:,:,:).
That is, my problem is the numbers of colon is varying because of the dimension.
Thanks in advance
You can use string arrays to index the array dynamically:
function out = extract(arr,dim,k)
subses = repmat({':'}, [1 ndims(arr)]);
subses(dim) = num2cell(k);
out = arr(subses{:});
where dim is the dimension in which you want to select and k is an index within that dimension.
I have used a code from this answer:
https://stackoverflow.com/a/27975910/3399825

Subtract a vector from a matrix (row-wise) [duplicate]

This question already has answers here:
Broadcast array multiplication in Fortran 90/95
(3 answers)
Closed 6 years ago.
Suppose I have a matrix A in fortran which is (n,m), and a vector B which is (1,m). I want to subtract the vector B from all rows of A without using a loop.
As of now I have only been able to do it with a loop:
PROGRAM Subtract
IMPLICIT NONE
REAL, DIMENSION(250,5) :: A
INTEGER, DIMENSION(1,5) :: B
INTEGER :: i
B(1,1) = 1
B(1,2) = 2
B(1,3) = 3
B(1,4) = 4
B(1,5) = 5
CALL RANDOM_NUMBER(A)
do i=1,250
A(i,:) = A(i,:) - B(1,:)
end do
end program
But this is very inefficient. E.g. in matlab one can do it in one line using the function reptmat.
Any suggestion on better way to do this?
You can use spread for this:
A = A - spread( B(1,:), 1, 250 )
Please note that Fortran is column-major, so B(1,:) is in general not contiguous in memory, and a temporary array is created.
[ It is in your case since you have only one column - but still worth mentioning. ]
In the same way, looping over the first index of A is inefficient.
It would probably speed things up a lot if you transposed your matrices.
Then, a loop solution might even be faster than the one using spread. (But this depends on the compiler. )

Count occurrences on a array using MATLAB [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Determining the number of occurrences of each unique element in a vector
I've the following array:
v = [ 1 5 1 6 7 1 5 5 1 1]
And I need to count the values and show the number that has more appearances.
From the example on the top, the solution would be 1 (there are five 1's)
Thanks in advance
Use mode.
If you need to return the number of elements as well, do the following:
m = mode(v);
n = sum(v==m);
fprintf('%d appears %d times\n',m,n);
Another method is using the hist function, if you're dealing with integers.
numbers=unique(v); %#provides sorted unique list of elements
count=hist(v,numbers); %#provides a count of each element's occurrence
Just make sure you specify an output value for the hist function, or you'll end up with a bar graph.
#Jacob is right: mode(v) will give you the answer you need.
I just wanted to add a nice way to represent the frequencies of each value:
bar(accumarray(v', 1))
will show a nice bar diagram with the count of each value in v.

Resources