Export certain columns from a Matlab matrix - matlab

I have a Matlab Matrix and would like to know if there is a way to extract certain columns from this to make a new matrix.
For example, if i have a matrix of;
data=1:20
I would like to export data from columns 1,2, 9,10 and make a new matrix file.
I would like to scale this up to a matrix of about 4,400 columns...so if there is a way to select columns at defined points (like every 8th and 9th column), then that would be super!
Any help would be greatly appreciated!
Thanks,
Aj

The example you've given can be done like this:
x=data([1,2,9,10]);
You can get every 8th column like this:
index=8;
x=data(index:index:end);
If you want every 8th and 9th column and to maintain the order:
index1=8;
index2=9;
x=data(sort([index1:index1:end index2:index2:end]));

if you also want to grab individual columns or rows similar process can be used and then concatenate the into a matrix
x=data(:,2) % get the 2nd column
y=data(:,8) % get the 8th
z=[x;y] or z=[x:y] % combine them
If you table is standard you can predefine them in a small script

Related

Matlab : How to extract elements in a uniform interval?

I have a (5160 X 4) matrix.
I want to extract just (1,1),(41,1),(81,1),(121,1)........ in a uniform interval, only from the first column of the matrix.
Assuming that data is your matrix, you can do this:
A = data(1:40:5160,1);
The 1:40:5160 will create an array such that it starts at 1, and goes up in increments of 40 as much as possible up until 5160. Once you create 1:40:5160, you can use this array and access the corresponding rows, and you are accessing the first column using the index of 1 for the second parameter. Actually, the last row that gets extracted is 5121. We aren't able to go up to 5161 due to the fact that your matrix has 5160 rows, and we have also specified 5160 as the ending of the indexing.
NB: This is very basic MATLAB syntax. Any standard MATLAB tutorial should teach you this.

Covariance for elements in a matrix - Matlab

I'm struggling here as it's my first attempt with Matlab...
I have data that looks like this:
The first row has stockID number and the 60 rows in each column contain the stock's returns.
I am trying to calculate the variance for each stock as well as a covariance matrix in Matlab. I am stuck because I do not know how to identify each column as its StockID. Should each column be its own variable? If so, how would I do this automatically as I have about 1,000 stocks...? Is there then a way to create a cov. matrix for each stock without manually entering in each variable, i.e. not do this: cov(10801, 12032, 13439, .....) ?
Thanks so much for the help!
You should be able to find the covariance by passing the second through 60th rows of your data into the cov function (covariance_matrix = cov(data(2:end,:))), as per this documentation. Hope that helps!

Creating a set matrix size

I have results which are 6 columns long however have been printed as 2 then 3 beneath then 1 beneath that! There are hundreds of lines and matlab will not except the structure of the matrix as it is now. Is there any way to tell matlab i want the first 5 results in their own columns then continuing down the rows after that?
My results appear as follows:
0.5 0
0.59095535915335684063 -0.59095535915335395405 -5.89791913085569763
33e-08
... repeated alot
thansk so much, em xx
I would just do a format shortE before you process the output, this will give you everything in scientific notation with 4 digits after the decimal. That 'should' allow you to fit your columns all in one line, so you don't have to deal with the botched output.
In general you should not want the output to be in a too specific format, but suppose you have this matrix:
M =[0.5 0 0.59095535915335684063 -0.59095535915335395405 -5.89791913085569763 33e-08];
To make it an actual matrix I will repeat it a bit:
M = repmat(M,10,1);
Now you can ensure that all six columns will fit on a normal screen by using the format.
format short
Try help format to find more options. Now simply showing the matrix will put all columns next to eachother. If you want one column below, the trick is to reduce your windows width untill it can only hold five columns. Matlab will now print the last column below the first.
M % Simply show the matrix
% Now reduce your window size
M % Simply show it again
This should help you display the numbers in matlab, if you want to process them further you can consider to write them to a file instead. Try help xlswrite for a simple solution.

Preserving matrix columns using Matlab brush/select data tool

I'm working with matrices in Matlab which have five columns and several million rows. I'm interested in picking particular groups of this data. Currently I'm doing this using plot3() and the brush/select data tool.
I plot the first three columns of the matrix as X,Y, Z and highlight the matrix region I'm interested in. I then use the brush/select tool's "Create variable" tool to export that region as a new matrix.
The problem is that when I do that, the remaining two columns of the original, bigger matrix are dropped. I understand why- they weren't plotted and hence the figure tool doesn't know about them. I need all five columns of that subregion though in order to continue the processing pipeline.
I'm adding the appropriate 4th and 5th column values to the exported matrix using a horrible nested if loop approach- if columns 1, 2 and 3 match in both the original and exported matrix, attach columns 4/5 of the original matrix to the exported one. It's bad design and agonizingly slow. I know there has to be a Matlab function/trick for this- can anyone help?
Thanks!
This might help:
1. I start with matrix 1 with columns X,Y,Z,A,B
2. Using the brush/select tool, I create a new (subregion) matrix 2 with columns X,Y,Z
3. I then loop through all members of matrix 2 against all members of matrix 1. If X,Y,Z match for a pair of rows, I append A and B
from that row in matrix 1 to the appropriate row in matrix 2.
4. I become very sad as this takes forever and shows my ignorance of Matlab.
If I understand your situation correctly here is a simple way to do it:
Assuming you have a matrix like so: M = [A B C D E] where each letter is a Nx1 vector.
You select a range, this part is not really clear to me, but suppose you can create the following:
idxA,idxB and idxC, that are 1 if they are in the region and 0 otherwise.
Then you can simply use:
M(idxA&idxB&idxC,:)
and you will get the additional two columns as well.

How to extract two columns from a matrix in a sequence of 6 columns in matlab

Basically I have a <96x659 double> matrix and I want to extract 1st and 2nd column , then 8th and 9th, then 15th and 16th column and so on..
So I want each 2 columns in a step of 6 . I hope I was clear enough. I'm newbie in matlab .
Thanks in advance!
All you really need to do is construct the list of columns you want:
columns = [1:7:size(matrix,2)+1, 2:7:size(matrix,2)+1];
submat = matrix(:, columns);
Keep in mind this will not necessarily return the columns in the order you want. If you want the columns in ascending order, you could substitute
submat = matrix(:, sort(columns));
Matlab Summary and Tutorial
This is a pretty decent introduction, if the Matlab documentation itself appears to be a little dense. Go through the examples, try them out.