Eigen3 Flatten Matrix as Vector - eigen3

In MATLAB, I can do the following
A = [1 2 3; 4 5 6];
A(:)
to get:
ans =
1
4
2
5
3
6
How would I do this with an Eigen3 Matrix?

The best way is to use Map:
Map<VectorXd> v(A.data(),A.size());
because in this case Eigen knows at compile time that you now have a 1D vector.
Of course, the result will depend on the storage order of A, that is, for a column major matrix (the default):
[1 4 2 5 3 6]^T
and for a row-major one:
[1 2 3 4 5 6]^T

Related

How to concatenate differently sized vectors in Julia?

How can I concatenate arrays of different size with a "filler" value where the arrays don't line up?
a = [1,2,3]
b = [1,2]
And I would like:
[1 2 3
1 2 missing]
Or
[1 2 3
1 2 nothing]
One way, using rstack which is "ragged stack". It always places arrays along one new dimension, thus given vectors, they form the columns of a matrix. (The original question may want the transpose of this result.)
julia> using LazyStack
julia> rstack(a, b; fill=missing)
3×2 Matrix{Union{Missing, Int64}}:
1 1
2 2
3 missing
julia> rstack(a, b, reverse(a), reverse(b); fill=NaN)
3×4 Matrix{Real}:
1 1 3 2
2 2 2 1
3 NaN 1 NaN

reshaping and re-arranging array using octave / matlab

I'm trying to reshape an array, perform an operation and then reshape it back to the original. See example of the output I'm trying to get. I can get a and b but I'm having trouble getting c to look like a again.
Step 1) (the original array)
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
Step 2) (reshape and perform some operation)
1,1,1,2,2,2,3,3,3,4,4,4,5,5,5
Step 3) (array is reshaped back to the original size to look like step 1) this is what I want
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
I can get the variables a and b but I'm not sure how to reshape c from b to look like a again see example code and output below
a=[repmat(1,[1,3]);repmat(2,[1,3]);repmat(3,[1,3]);repmat(4,[1,3]);repmat(5,[1,3])]
[rw,col]=size(a)
b=reshape(a',1,rw*col)
c=reshape(b,rw,col)
a=
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
b=1,1,1,2,2,2,3,3,3,4,4,4,5,5,5
c =
1 2 4
1 3 4
1 3 5
2 3 5
2 4 5
Ps: I'm using Octave 4.0 which is like Matlab.
MATLAB and Octave use column-major ordering so you'll need to reshape the result with that in mind. The data will be filled down the columns first but you want it to fill the columns first. To achieve this, you can specify the number of columns as the number of rows provided to reshape and then transpose the result
c = reshape(b, 3, []).'
Or more flexibly
c = reshape(b, flip(size(a))).'

average operation in the first 2 of 3 dimensions of a matrix

Suppose A is a 3-D matrix as below (2 rows-2 columns-2 pages).
A(:,:,1)=[1,2;3,4];
A(:,:,2)=[5,6;7,8];
I want to have a vector, say "a", whose inputs are the average of diagonal elements of matrices on each page. So in this simple case, a=[(1+4)/2;(5+8)/2].
But I have difficulties in matlab to do so. I tried the codes below but failed.
mean(A(1,1,:),A(2,2,:))
You can use "partially linear indexing" in the two dimensions that define the diagonal, as follows:
Since partially linear indexing can only be applied on trailing dimensions, you first need to apply permute to rearrange dimensions, so that the first and second dimensions become second and third.
Now you leave the first dimension untouched, linearly-index the diagonals in the second and third dimensions (which effectly reduces those two dimensions to one), and apply mean along the (combined) second dimension.
Code:
B = permute(A, [3 1 2]); %// step 1: permute
result = mean(B(:,1:size(A,1)+1:size(A,1)*size(A,2)), 2); %// step 2: index and mean
In your example,
A(:,:,1)=[1,2;3,4];
A(:,:,2)=[5,6;7,8];
this gives
result =
2.5000
6.5000
You can use bsxfun for a generic solution -
[m,n,r] = size(A)
mean(A(bsxfun(#plus,[1:n+1:n^2]',[0:r-1]*m*n)),1)
Sample run -
>> A
A(:,:,1) =
8 4 1
7 6 3
1 5 8
A(:,:,2) =
1 7 6
8 5 2
1 2 7
A(:,:,3) =
6 2 8
1 1 6
1 4 5
A(:,:,4) =
8 1 6
1 5 1
9 2 7
>> [m,n,r] = size(A);
>> sum(A(bsxfun(#plus,[1:n+1:n^2]',[0:r-1]*m*n)),1)
ans =
22 13 12 20
>> mean(A(bsxfun(#plus,[1:n+1:n^2]',[0:r-1]*m*n)),1)
ans =
7.3333 4.3333 4 6.6667

Replicate Element-wise in matrix [duplicate]

This question already has answers here:
Element-wise array replication in Matlab
(7 answers)
A similar function to R's rep in Matlab [duplicate]
(4 answers)
Closed 8 years ago.
Let's say, I have:
A=[1 2; 3 4];
I want to use repmat that return:
B = [1 1 2 2; 1 1 2 2; 3 3 4 4; 3 3 4 4]
Kindly need your help. Thank you
I do not know a method using repmat but here is a method using kron
kron([1 2 ; 3 4],[1 1;1 1])
ans =
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
An alternative which uses repmat is
A=[1 2; 3 4];
cell2mat(arrayfun(#(x)repmat(x,2,2),A,'UniformOutput',false))
ans =
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
arrayfun is used to evaluate each element in A using the anonymous function #(x)repmat(x,2,2) which replicates that single element into a 2x2 matrix.
The result of arrayfun is a 2x2 cell array where each element is a 2x2 matrix. We then convert this cell array into a matrix via cell2mat.
Let the data be defined as
A = [1 2; 3 4];
R = 2; %// number of repetitions of each row
C = 2; %// number of repetitions of each column. May be different from R
Two possible approaches are as follows:
The simplest method is to use indexing:
B = A(ceil(1/R:1/R:size(A,1)), ceil(1/C:1/C:size(A,2)));
If you really want to do it with repmat, you need to play with dimensions using permute and reshape: move original dimensions 1, 2 to dimensions 2, 4 (permute); do the repetition along new dimensions 1, 3 (repmat); collapse dimensions 1, 2 into one dimension and 3, 4 into another dimension (reshape):
[r c] = size(A);
B = reshape(repmat(permute(A, [3 1 4 2]), [R 1 C 1]), [r*R c*C]);
Example result for R=2, C=3 (obtained with any of the two approaches):
B =
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4

Merge arrays with unequal number of columns

I have around 100 1D arrays I'd like to merge to a matrix.
The arrays have 140 to 180 columns.
Is it possible to merge these 1 x (140-180) arrays to a matrix with a dimension of 100 (amount of arrays) x 180 ?
All the arrays contain numbers. I want to expand the 1x140 array to a 1x180 array by means of interpolation.
In a simplified form, it should be something like this:
A = [1 5 7 8 3]
B = [1 3 5]
result=
[1 5 7 8 3
1 2 3 4 5]
The array B (1x3) is expanded to an 1x5 matrix. And the values in between are interpolated.
Basically, I thought of using "vertcat" after all arrays are expanded by a same amount of columns.
Thanks in advance,
Koen
How about this?
array = {[1 5 7 8 3],[1 3 5]}; % example data
N = 5; % desired length (180 in your case)
aux = cellfun(#(v) interp1(linspace(0,1,length(v)),v,linspace(0,1,N)), array, 'uni', false);
result = cat(1,aux{:});
It uses linear interpolation. For your example, this gives
>> result
result =
1 5 7 8 3
1 2 3 4 5
Note that linear interpolation modifies all values of the vector except first and last, in general. For example, with N=5 the vector [1 3 4 5] would get interpolated to [1 2.5 3.5 4.25 5]. You could use other forms of interpolation by passing an extra argument to interp1, see help interp1.

Resources