Array Dimension - arrays

I am currently learning to use Keras in R. When I run the command
dim(mnist$train$x)
I get the output as (output 1)
[1] 60000 28 28
which means that there are 60000 matrices each with a 28*28 dimension.
Now when I create an array through R code for the same dimension , I use
test <- array(28*28*6000,dim=c(28,28,6000))
where the inner layers are specified first and upon using the statement dim(test) I get this output (output 2)
[1] 28 28 6000
Both these notations are showing up the same array in different format. Is it possible to get the output in the second case in the output 1 format ?

Do you just want output 2 in output 1 format? You mean something like this:
test <- array(28*28*6000,dim=c(28,28,6000))
d <- dim(test)
newdim <- c(d[length(d)], d[-length(d)] )
newdim will be 60000 28 28

Related

Forecasting in gretl

Consider the following gretl script (hansl):
open bjg.gdt
arima 1 1 0 ; 2 1 0 ; g
series fitted = $yhat
g1 <- gnuplot g fitted --with-lines --time-series --output=display
What I want to do next is to make a forecast for, lets say, 24 steps ahead, that is from Jan 1961 to Dec 1962. I believe the fifth line should be something like
fcast [options] --plot=display
What options to use here? I have tried several combinations but none is successful.
After further experimentation, here is the solution:
open bjg.gdt
arima 1 1 0 ; 2 1 0 ; g
series fitted = $yhat
g1 <- gnuplot g fitted --with-lines --time-series --output=display
dataset addobs 24
g2 <- fcast --dynamic --out-of-sample --plot=display

Array sorting in matlab

Hi i have a 289x2 array that i want to sort in MatLab. I want to sort the first column into numerical ascending order. However I want to keep the second column entry that is associated with it. Best way to explain is through an example.
x = 76 1
36 2
45 3
Now I want to sort x so that it returns an array that looks like:
x = 36 2
45 3
76 1
So the first column has been sorted into numerical order but has retained its second column value. So far I have tried sort(x,1). This sorts the first column as i want but does not keep the pairing. This returns x as:
x = 36 1
45 2
76 3
Any help would be great. Cheers!!
This is exactly what sortrows does.
x=sortrows(x); % or x=sortrows(x,1);
or if you want to use sort then get the sorted indexes first and then arrange the rows accordingly like this:
[~, idx] = sort(x); %Finding the sorted indexes
x = x(idx(:,1),:) ; %Arranging according to the indexes of the first column
Output for both approaches:
x =
36 2
45 3
76 1

How is an array sliced?

I have some sample code where the array is sliced as follows:
A = X(:,2:300)
What does this mean about the slice of the array?
: stands for 'all' if used by itself and 2:300 gives an array of integers from 2 to 300 with a spacing of 1 (1 is implicit) in MATLAB. 2:300 is the same as 2:1:300 and you can even use any spacing you wish, for example 2:37:300 (result: [2 39 76 113 150 187 224 261 298]) to generate equally spaced numbers.
Your statement says - select every row of the matrix A and columns 2 to 300. Suggested reading

Not able to extract data from R array

I am new to R. I have an R array (or atleast I think) which gives the following output
> head(x)
[,1] [,2]
199 3.40 3.50
What is the 199 on the front mean? How do I extract the elements of this array?
As above, the 199 is a row name. You extract elements from a data frame (or vector) in R by using square brackets:
x[,1] # gives a column
x[1,] # give a row
x[1:2,] # gives several rows
You can also use column names like so:
x <- data.frame(col1 = c(1,2,3), col2 = c("A", "B", "C"))
x$col1 # 1 2 3
You'll figure out more as you start to play around in R and do some R tutorials.

Intertwining 3 arrays in matlab / octave to get correct pattern

I know I can intertwine 2 arrays by
C = [A(:),B(:)].'; %'
D = C(:)
But how can I intertwine 3 arrays with a (pendulum type of pattern going back and forth) See image below with arrows showing the intertwining path pattern I'm trying to get (each column is an array). Also the number pattern I'm trying to get is also next to it, in one large column. Please note the numerical values are just examples to make it easier to read. the numerical values could be decimals also
I tried the code below but the pattern is incorrect.
A=[1,2,3,4,5]
B=[10,20,30,40,50,60,70,80,90]
C=[100,200,300,400,500]
D = [A(:),B(:),C(:)].'; %'
E = D(:)
I get an error in the D array due to the fact that the B array is a larger size than A and C but the number pattern is also not following the pattern I'm trying to get.
1
10
100
2
20
200
3
30
300
4
40
400
5
50
500
error: horizontal dimensions mismatch (5x1 vs 9x1)
The pattern from the 3 arrays I'm trying to get is below.
Please note the numerical values are just examples to make it easier to read. the numerical values could be decimals also
1
10
100
20
2
30
200
40
3
50
300
60
4
70
400
80
5
90
500
PS: I'm using Octave 3.8.1 which is like matlab
Have you tried the following?
D = zeros(4 * size(A, 2) - 1, 1); % initialization
D(1 : 4 : end) = A;
D(2 : 2 : end) = B;
D(3 : 4 : end) = C;

Resources