What does the # operator do between two 2D arrays [duplicate] - arrays

This question already has answers here:
What does the "at" (#) symbol do in Python?
(14 answers)
What is the '#=' symbol for in Python?
(3 answers)
Closed 3 months ago.
I'm studying a code for 2D correlation spectroscopy.
The code takes 2 arrays (a and b) with the same numbers of rows, and calculated the pair-wise relationship between all the possible combination of columns in the two arrays. The operand to perform such calculation is "#".
For example:
arr1=np.array([[1,2,3], [2,3,4]])
arr2=np.array([[1,2,2,6], [3,4,5,7]])
Final_array = arr1.T # arr2 / (len(arr1) - 1)
I'm not able to an explanation of what the # operand does.

Related

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{:});

Matlab: convert (i,j) matrix to (1,j) array [duplicate]

This question already has answers here:
the easiest way to convert matrix to one row vector [duplicate]
(2 answers)
How do you concatenate the rows of a matrix into a vector?
(2 answers)
Closed 5 years ago.
I got a 365x24 matrix (hours of the day x days of the year) and I'd like to convert it to a 1x8760 matrix (for all the hours of the year).
So basically, every row of the original matrix should be copy pasted after the previous row.
How can this be done?
Thanks!
For any matrix, the (:) indexing operation concatenates the columns of the matrix to form a vector.
>> a = [1,2,3; 4,5,6];
>> a = a(:)
ans =
1
4
2
5
3
6
In your case, you want the rows concatenated. To achieve this, simply transpose the matrix before indexing with (:). Finally, you can simply transpose it to get the row vector.

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

How to convert cell array of string numbers into numerical vector [duplicate]

This question already has answers here:
Convert cell to array in matlab
(3 answers)
Closed 6 years ago.
I am using Matlab and I have the following data:
a = {'3' '11' '7'}; % This is what I have.
My goal is to use a function to convert such cell into a vector having the following shape:
b = [3 11 7]; % What I need to get!
If I try to use the b = cell2mat(a) function the result that I get is this number: b = 3117; which is not what I want.
What function should I use to reach my goal? If possible please do not use for loops but just Matlab functions.
You're looking for str2double:
b = str2double(a);

Java 8 frequency Object in array [duplicate]

This question already has answers here:
Word frequency count Java 8
(11 answers)
Closed 7 years ago.
I have an Object[] array
I need to create map Map<Obejct, Integer>, where Integer value contains frequency of key Object in array.
How can i do it in java 8 style, using Collectors?
You can do (I hope I don't have any typos) :
Map<Object,Long> map = Stream.of(array)
.collect(Collectors.groupingBy(o -> o,
Collectors.counting()));
This should group the elements of the array by equality and count the number of Objects in each group.

Resources