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!
Related
I have a row cell vector M, containing matrices in each cell. Every matrix m (matrix inside the big matrix M) is made of 2 channels (columns), of which I only want to use the first.
The approach I thought about was going through each m, check if it has 2 channels, and if that is the case delete the second channel.
Is there a way to just slice it in matlab? or loop it and obtain the matrix M as the matrix m would disappear.
First code is:
load('ECGdata.mat')
I have the below.
when I double-click in one of the variable , here is what I can see:
As you can see the length of each matrix in each cell is different. Now let's see one cell:
The loop I'm trying to get must check the shape of the matrix (I'm talking python here/ I mean if the matrix has 2 columns then delete the second) because some of the variables of the dataframe have matrix containing one column (or just a normal column).
In here I'm only showing the SR variable that has 2 columns for each matrix. Its not the case for the rest of the variables
You do not need to delete the extra "channel", what you can do is quite simple:
newVar = cellfun(#(x)x(:,1), varName, 'UniformOutput', false);
where varName is SR, VF etc. (just run this command once for each of the variables you load).
What the code above does is go over each element of the input cell (an Nx2 matrix in your example), and select the first column only. Then it stores all outputs in a new cell array. In case of matrices with a single column, there is no effect - we just get the input back.
(I apologize in advance if there is some typo / error in the code, as I am writing this answer from my phone and cannot test it. Please leave a comment if something is wrong, and I'll do my best to fix it tomorrow.)
this is a very basic problem but I didn't find any hints on it. Let's say I have a 2x4 matrix and I want to reduce the dimension of the matrix to only these columns that are in the sum larger than 1:
A=rand(2,4)
ind = sum(A,1).>1
That gives me an indicator of the columns I want to retain. Naively one would assume that I can do that:
A[:,ind]
which doesn't work as ind is a BitArray and only for Bool Arrays this is allowed, i.e., the following works
A[:,[true,true,false,true]]
in return, the following does work:
A[A.>0.5]
But it returns a vector of filtered elements.
What is the logic behind this and how do I solve my problem?
As noted in the comments, this is fixed by using a version of Julia which is >=v0.4.
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
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.
I have a 161*32 matrix (labelled "indpic") in MATLAB and I'm trying to find the frequency of a given number appearing in a row. So I think that I need to analyse each row separately for each value, but I'm incredibly unsure about how to go about this (I'm only new to MATLAB). This also means I'm incredibly useless with loops and whatnot as well.
Any help would be greatly appreciated!
If you want to count the number of times a specific number appears in each row, you can do this:
sum(indpic == val, 2)
where indpic is your matrix (e.g image) and val is the desired value to be counted.
Explanation: checking equality of each element with the value produces a boolean matrix with "1"s at the locations of the counted value. Summing each row (i.e summing along the 2nd dimension results in the desired column vector, where each element being equal to the number of times val is repeated in the corresponding row).
If you want to count how many times each value is repeated in your image, this is called a histogram, and you can use the histc command to achieve that. For example:
histc(indpic, 1:256)
counts how many times each value from 1 to 256 appears in image indpic.
Like this,
sum(indpic(rownum,:) == 7)
obviously change 7 to whatever.
You can just write
length(find(indpic(row_num,:)==some_value))
and it will give you the number of elements equal to "some_value" in the "row_num"th row in matrix "indpic"