Reshape array in octave / matlab - matlab

I'm trying to reshape an array but I'm having some issues.
I have an array see image below and I'm trying to get it to look like / follow the pattern in the row highlighted in yellow. (note: I'm not trying to calculate the array but reshape it so it follows a pattern)
aa=[1:5;10:10:50;100:100:500]
aa_new=reshape(aa',[1 numel(aa)])
aa_new produces:
1 2 3 4 5 10 20 30 40 50 100 200 300 400 500
I'm trying to get:
1 2 3 4 5 50 40 30 20 10 100 200 300 400 500

Reverse the column numbers of every second row i.e.
aa(2:2:end,:) = aa(2:2:end, end:-1:1);
Now you're good to go with reshaping:
aa = reshape(aa.', 1, []);

Related

Reshaping 3 dimensional array to 2 by stacking matrixes horizontally [duplicate]

This question already has an answer here:
Reshape matrix from 3d to 2d keeping specific order
(1 answer)
Closed 5 years ago.
I have 4x3x2 array:
A(:,:,1) =
1 10 100
2 20 200
3 30 300
4 40 400
A(:,:,2) =
5 50 500
6 60 600
7 70 700
8 80 800
I want to reshape it to B matrix with size 8x3 preserving the structure of each matrix as:
B =
1 10 100
2 20 200
3 30 300
4 40 400
5 50 500
6 60 600
7 70 700
8 80 800
Any idea how to do it in a simple and neat way?
As seen here.
Method 1: permute and reshape
B = reshape(permute(A, [2 1 3]), size(A, 2), [])'
Method 2: cell -> matrix
B = num2cell(A, [1 2]);
B = vertcat(B{:})

kdb update multiple columns corresponding to multiple where clauses

I have a table, which is :
t:([]a:1 3 2 1 2 3 3 2 1;b:10 20 30 40 50 60 70 80 90;c:100 200 300 400 500 600 700 800 900)
And I want all c to be 0 where a is equal to 2, and all be to be 0 where a is equal to 1.
Currently I have these two codes:
t:update b:0 from t where a=1
t:update c:0 from t where a=2
My question is how to combine these two lines of codes into one. Because I am working on a table which is far bigger than this simple example and it will take me a lot of rows of codes to do all the updates, which is too long.
You can use vector conditional for this:
update b:?[a=1;0;b], c:?[a=2;0;c] from t

Matlab loop to convert N x 1 matrix to 60 x 4718 matrix

I am a novice at Matlab and am struggling a bit with creating a loop that will a convert a 283080 x 2 matrix - column 1 lists all stockID numbers (each repeated 60 times) and column 2 contains all lagged monthly returns (60 observations for each stock) into a 60 x 4718 matrix with a column for each stockID and its corresponding lagged returns falling in 60 rows underneath each ID number.
My aim is to then try to calculate a variance-covariance matrix of the returns.
I believe I need a loop because I will be repeating this process over 70 times as I have multiple data sets in this same current format
Thanks so much for the help!
Let data denote your matrix. Then:
aux = sortrows(data,1); %// sort rows according to value in column 1
result = reshape(aux(:,2),60,[]); %// reshape second column as desired
If you need to insert the stockID values as headings (first row of result), add this as a last line:
result = [ unique(aux(:,1)).'; result ];
A simple example, replacing 60 by 2:
>> data = [1 100
2 200
1 101
2 201
4 55
3 0
3 33
4 56];
>> aux = sortrows(data,1);
>> result = reshape(aux(:,2),2,[])
>> result = [ unique(aux(:,1)).'; result ];
result =
1 2 3 4
100 200 0 55
101 201 33 56

display a numeric mat as dataset (with Row/Col Names) in MATLAB

I'm not very familiar with datasets / displaying data in a readable format in Matlab. I have a numeric matrix with say 4 cols and I need to display it as:
mydata1 = [100 200 400 40] ;
mydata2 = [1 2 3 4 ] ;
display it as -->
CovA CovB CovC CovD
Sys1 100 200 400 40
Sys2 1 2 3 4
I am guessing datasets would help me implement this. Once converted, I'm hoping to publish this dataset with others. Thanks!
DATASET constructs data column-wise. Consequently, you can either accept that everything is transposed, i.e.
ds = dataset({mydata1','sys1'},{mydata2','sys2'},'obsNames',...
{'CovA','CovB','CovC','CovD'})
ds =
sys1 sys2
CovA 100 1
CovB 200 2
CovC 400 3
CovD 40 4
or you construct the dataset rather inconveniently by first catenating mydata:
mm = [mydata1;mydata2];
dataset(mm(:,1),mm(:,2),mm(:,3),mm(:,4),'varNames',...
{'CovA','CovB','CovC','CovD'},'obsNames',{'sys1','sys2'})
ans =
CovA CovB CovC CovD
sys1 100 200 400 40
sys2 1 2 3 4
This will display a matrix as a table in matlab
http://www.mathworks.com/matlabcentral/fileexchange/27384-disptable-display-matrix-with-column-or-row-labels

How can I divide each row of a matrix by a fixed row?

Suppose I have a matrix like:
100 200 300 400 500 600
1 2 3 4 5 6
10 20 30 40 50 60
...
I wish to divide each row by the second row (each element by the corresponding element), so I'll get:
100 100 100 100 100 100
1 1 1 1 1 1
10 10 10 10 10 10
...
Hw can I do it (without writing an explicit loop)?
Use bsxfun:
outMat = bsxfun (#rdivide, inMat, inMat(2,:));
The 1st argument to bsxfun is a handle to the function you want to apply, in this case right-division.
Here's a couple more equivalent ways:
M = [100 200 300 400 500 600
1 2 3 4 5 6
10 20 30 40 50 60];
%# BSXFUN
MM = bsxfun(#rdivide, M, M(2,:));
%# REPMAT
MM = M ./ repmat(M(2,:),size(M,1),1);
%# repetition by multiplication
MM = M ./ ( ones(size(M,1),1)*M(2,:) );
%# FOR-loop
MM = zeros(size(M));
for i=1:size(M,1)
MM(i,:) = M(i,:) ./ M(2,:);
end
The best solution is the one using BSXFUN (as posted by #Itamar Katz)
You can now use array vs matrix operations.
This will do the trick :
mat = [100 200 300 400 500 600
1 2 3 4 5 6
10 20 30 40 50 60];
result = mat ./ mat(2,:)
which will output :
result =
100 100 100 100 100 100
1 1 1 1 1 1
10 10 10 10 10 10
This will work in Octave and Matlab since R2016b.