I have a set of data whereby there is a column which can take the value of "BUYS" or "SELLs" and I have another column where the quantity is displayed (shown in absolute terms). I want to be able to query this data and make sure that when the value = "SELLS" I am multiplying the quantity by -1.
thanks
You could try using a vector conditional?
https://code.kx.com/q4m3/10_Execution_Control/#1013-vector-conditional-evaluation
Related
In Matlab, I am given a table with two columns. Now I want to find the corresponding value of the left column to the maximum value of the right column:
P1_sat = P1(ismember(P2,max(P2)))
This works, however, the maximum is identical at 3 values of the left column, P1. These 3 values are right next to each other. So I want to consider the mid value. Is there a "consider the mid value" - command?
Adding the following code line will complete my goal:
P1_sat = sum(P1_sat,1) / length(P1_sat)
The three different values of P1, whose corresponding value of P2 is the maximum of P2, are added and then divided by 3. This gives the average value, which is also the mid point.
My goal is to create a random, 20 by 5 array of integers, sort them by increasing order from top to bottom and from left to right, and then calculate the mean in each of the resulting 20 rows. This gives me a 1 by 20 array of the means. I then have to find the column whose mean is closest to 0. Here is my code so far:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
MeanArray= mean(transpose(NewArray(:,:)))
X=min(abs(x-0))
How can I store the column number whose mean is closest to 0 into a variable? I'm only about a month into coding so this probably seems like a very simple problem. Thanks
You're almost there. All you need is a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
ColNum = find(abs(mean(NewArray,1))==min(abs(mean(NewArray,1)))); %// gives you the column number of the minimum
MeanColumn = RandomArray(:,ColNum);
find will give you the index of the entry where abs(mean(NewArray)), i.e. the absolute values of the mean per column equals the minimum of that same array, thus the index where the mean of the column is closest to 0.
Note that you don't need your MeanArray, as it transposes (which can be done by NewArray.', and then gives the mean per column, i.e. your old rows. I chucked everything in the find statement.
As suggested in the comment by Matthias W. it's faster to use the second output of min directly instead of a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
[~,ColNum] = min(abs(mean(NewArray,1)));
MeanColumn = RandomArray(:,ColNum);
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
I have two columns in .std file. I want average of the second column values corresponding to all values ranging from some value (eg. 1.0- 1.9) in first column how can I program in Matlab?
Say, a is the name of your two column matrix. If you want to find all of the values in the first column in the range of 1.0 - 1.9 and then use those entries to find the mean in the second column you can do this:
f = find(a(:,1)>=1 & a(:,1)<=1.9)
m = mean(a(f,2))
find will find the values that lie within this range and return the index, and a(f,2) accesses those indices in the in the second column and takes the mean. You can also do it with one line like so:
m = mean(a((a(:,1)>=1 & a(:,1)<=1.9),2))
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.