Im new to Matlab and this seems to be beyond me. Appreciate the help and thanks in advance.
Basically, I have a multiple columns dataset with column headers. Column numbers could vary from dataset to dataset.
Need to iterate through all the combinations of columns (eg A+B, A+C....B+C, B+D...etc) and run a formula (in this instance it is a correlation formula but could be another formula subsequently).
If particular combination returns "true", then the column headers of the pair will be returned.
Would appreciate if you could point me in the right direction.
Thanks in advance.
Use nchoosek to get all pairs of columns:
pairs_columns = nchoosek(1:m, 2);
pairs = {};
for pair = 1:size(pairs_columns,1)
flag = your_correlation_test(data(:,pairs_columns(pair,1)), data(:,pairs_columns(pair,2)));
if flag
pairs{end+1,1} = data_header(pairs_columns(pair,1));
pairs{end,2} = data_header(pairs_columns(pair,2)); %// Note that you don't need end+1 anymore as the previous line will have already increased the number of rows in the vector
end
end
m is your number of columns
your_correlation_test is your test function that returns a Boolean result
data is your dataset (which I'm assuming you can index by column number?)
data_header is a place holder for whatever the correct way to get the header is from your dataset based on the column number. Sorry I'm not very familiar with datasets in Matlab
Related
I have a data matrix Data(8765x138) that first and second columns are x and y coordinates. I have sampled some specific points in another array, Points(2000x2), first and second columns in A refers to x and y, respectively. I want to extract some specific rows in Matlab that match with matrix A (both x,y). The output should be (2000x138). I tried the following code but the result is not correct.
newData = Data(ismember(Data(:,1),Points(:,1))& ismember(Data(:,2),Points(:,2)),:);
what should I do to select the rows from Data that its first and second columns match to my Points matrix. Someone please help, I feel like I've tried everything!
You can use ismember with argument rows
newData = Data(ismember(Data(:,1:2),Points(:,1:2),'rows'),:);
But when you are sampling it is better to save index of sampled data and use them to extract coordinates.
It's a badly worded question, so hard to know for sure, but the solution might be to use ismember() to find matching rows, e.g.:
DataXY = Data(:,[1,2]);
tf = ismember(DataXY, Points, 'rows');
newData = Data(tf,:);
I have loaded an xlsx file into Matlab using the
data = xlsread()
Now there is a column which I would like to filter as per positive and negative values in the cells.
How would I go about this?
I am just starting, if someone can point out a good resource to learn how to program/code in matlab, I would be very grateful.
Thanks
col = data(:,3);
gtz = col(col>0);
ltz = col(col<0);
eqz = col(col==0);
gives you the greater then zero, lower then zero and equal to zero values in column 3.
And searchich for 'Matlab tutorial' in your favorite search engine will bring you heaps of them.
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!
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"
I need this section of my code to run faster, as it is called many many times. I am new to Matlab and I feel as though there MUST be a way to do this that is not so round-about. Any help you could give on how to improve the speed of what I have or other functions to look into that would help me perform this task would be appreciated.
(Task is to get only lines of "alldata" where the first column is in the set of "minuteintervals" into "alldataMinutes". "minuteintervals" is just the minimum value of "alldata" column one increasing by twenty to the maximum of alldata.
minuteintervals= min(alldata(:,1)):20:max(alldata(:,1)); %20 second intervals
alldataMinutes= zeros(30000,4);
counter=1;
for x=1:length(alldata)
if ismember(alldata(x,1), minuteintervals)
alldataMinutes(counter,:)= alldata(x,:);
counter= counter+1;
end
end
alldataMinutes(counter:length(alldataMinutes),:)= [];
This should give you what you want, and it should be substantially faster:
minuteintervals = min(alldata(:,1)):20:max(alldata(:,1)); %# Interval set
index = ismember(alldata(:,1),minuteintervals); %# Logical index showing first
%# column values in the set
alldataMinutes = alldata(index,:); %# Extract the corresponding rows
This works by passing a vector of values to the function ISMEMBER, instead of passing values one at a time. The output index is a logical vector the same size as alldata(:,1), with a value of 1 (i.e. true) for elements of alldata(:,1) that are in the set minuteintervals, and a value of 0 (i.e. false) otherwise. You can then use logical indexing to easily extract the rows corresponding to the ones in index, placing them in alldataMinutes.