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

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.

Related

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

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.

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

Address each Matlab array element, one by one, independent from dimension (row vs. column) [duplicate]

This question already has an answer here:
Why, if MATLAB is column-major, do some functions output row vectors?
(1 answer)
Closed 7 years ago.
I would like to do something with each element in an array. For a row array, I can do this:
array = [1 2 3];
i = 0;
for a = array
i = i + 1;
end
fprintf('Number of iterations: %g\n', i)
Number of iterations: 3
It will output 3, so it actually accessed each array element one after another.
However if the array is a column, the same code will output just 1:
array = [1; 2; 3];
...
Number of iterations: 1
I wonder why exactly this happens and if there is a way to iterate through an array, independent from its "directional dimension" and without using for i = 1:numel(array), a = array(i).
When a for loop is initialized with an array, it iterates column by column. This may not be what you want, but that the built in behavior (see http://www.mathworks.com/help/matlab/ref/for.html).
You can force your matrix into a linear row vector, so MATLAB will iterate the elements 1 by 1 with the following:
for i = A(:)'
i % use each value of A
end
Normally, a combination of vector operations will be faster than a for loop, so only use a for loop when you can't think of the appropriate vector operation equivalent.

Storing multiple powers of matrices in matlab [duplicate]

This question already has answers here:
How to generate the first twenty powers of x?
(4 answers)
Closed 7 years ago.
I have 1000 matrices with dimensions 2x2.
What I now need to do is to get 30 consecutive powers of those matrices (A^2, A^3... ...A^30) and store them all.
I found a topic that suggested using bsxfun:
Vectorizing the creation of a matrix of successive powers
However, bsxfun does not work with cell arrays ("Error using bsxfun
Operands must be numeric arrays").
What can I do?
PS. A secondary question: once I have them, I want to plot 4 graphs (each corresponding to 1 of the elements of the 2x2 matrices) with 30 positions (x-axis) which will show confidence bands (16th and 84th percentile).
EDIT: Someone linked to a question that was similar to the one that I linked. From what I can understand, the question there is about a vector, not array of matrices.
Assuming your array A is 2-by-2-by-1000, here are two loops to make things work:
A = rand(2,2,1000);
K = 30;
%%
N = size(A,3);
APower = zeros(2,2,N,K);
APower(:,:,:,1) = A;
for i = 1:N
for k = 2:K
APower(:,:,i,k) = A(:,:,i)*APower(:,:,i,k-1);
%// Alternatively you could use:
%APower(:,:,i,k) = A(:,:,i)^k;
end
end
You need to replicate the matrix 30 times to do this using cellfun. For example,
a = repmat(A{1},1,30);% Select one of your A matrices
b = num2cell(1:30);
x = cellfun(#(a,b) a^b,a,b,'UniformOutput',false)
Since you need to run cellfun for each element of A another way is to use arrayfun as below.
a = A{1};
b = 1:30;
x = arrayfun(#(b) a^b,b,'UniformOutput',false)

Resources