Rank values in a dataset row - matlab

I have a dataset of stock prices called 'stocks'. Each column is a different stock. Each row is the date of the stock prices.
How can I rank the stock price of a given date?
I tried
tiedrank(stocks.yhoo)
And it successfully ranked the prices of YHOO stock. However, I would like to rank by row, not column.
Also, when I tried
tiedrank(stocks(1,:))
or to delete the date column in column 1
tiedrank(stocks(1,2:3))
I got the error message: Dataset array subscripts must be two-dimensional.
Am I doing something wrong? Or am I better off using matrices?

If I understand correctly, you want to rank the stocks according to price at a given date, where dates are rows, and stocks are columns. To use tiedrank across a row, you need to convert that part of the dataset to double, and then use the output index list to sort:
%# create index for sorting
idx = tiedrank( double( stocks(1,:) ));
%# reorder columns with index
sortedStocks = stocks(:,idx);

Related

Sorting out with highest values first

for (index, item) in users.enumerated(){
//countUserIds[item.name] = [Float(item.id),avgs[index]]
print("Name:",item.name,"ID:",item.id,"Score:",avgs[index])
}
Im trying to sort my output by having the highest averages listed first, but if I sort my array it will not be linked to the right name and ID. How can I sort the averages and display the users with the highest average first?

is there a query formula to count the latest value in a specific date for a specific id along with all the unique ids's values?

For example, post id 18158935492035927 has value of 42 in Col D in date 08/24 and the same id# has value of 44 in date 08/25.
For date 08/25, is there a query formula to scan all the values in the Col D to count the value of 44 and not 42 in 08/25 for this same post id along with all the unique post ids' values?
Like from 08/24 to 08/25 there's total of 3 unique post ids. For date 08/25, is there a formula to scan all rows in Col D to sum values of (34+0+44) and not (34+42) from col D?
First, it would help you if you were to use a cell (like E2) where you can store the latest date in column A
=max(A3:A9)
Following that, you can use this formula
=QUERY(A3:D9,"select sum(D)
where A=DATE '"&TEXT(E2, "yyyy-mm-dd")&"'
label sum(D) '' ")
(Please adjust ranges to your needs)

Calculate median sales price with using 3 variables Tableau 10

I would like calculate the median sales price and the median rental price for an apartment in NYC in each of the 5 boroughs, Brooklyn, Bronx Manhattan, Queens and Staten Island. In Tableau the sales and and rentals are groups of ListPrice -- Variables ListPrice is NUMBER(decimal) Type (includes Sales & Rentals, Borough
Any help is appreciated
I tried using Tableau's table calculation feature but that did not work, I tried
WINDOW_MEDIAN(SUM([ListPrice])-1, -1)
ERROR: WINDOW_MEDIAN is being called with (float, integer), did mean
(float,integer,integer)
Data
Type Borough ListPrice
RentalType1 Manhattan $5,000
RentalType2 Bronx $3,000
RentalType2 Brooklyn $3,000
SalesType2 Manhattan $900,000
SalesType1 Brooklyn $100,000
SalesType1 Bronx $500,000
SalesType2 Queens $800,000
SalesType2 Staten Island $400,000
Table calculations takes 3 arguments, Expression, First row of the partition and last row of the partition. In your formula you haven't given last row of the partition.
Run the function for type in each Borough and calculate for each Borough.
So your formula would be:
WINDOW_MEDIAN(SUM(INT([List Price])),FIRST(),LAST())
are you looking to get values below:
Here calculation2 is median value

Min value with GROUP BY in Power BI Desktop

id datetime new_column datetime_rankx
1 12.01.2015 18:10:10 12.01.2015 18:10:10 1
2 03.12.2014 14:44:57 03.12.2014 14:44:57 1
2 21.11.2015 11:11:11 03.12.2014 14:44:57 2
3 01.01.2011 12:12:12 01.01.2011 12:12:12 1
3 02.02.2012 13:13:13 01.01.2011 12:12:12 2
3 03.03.2013 14:14:14 01.01.2011 12:12:12 3
I want to make new column, which will have minimum datetime value for each row in group by id.
How could I do it in Power BI desktop using DAX query?
Use this expression:
NewColumn =
CALCULATE(
MIN(
Table[datetime]),
FILTER(Table,Table[id]=EARLIER(Table[id])
)
)
In Power BI using a table with your data it will produce this:
UPDATE: Explanation and EARLIER function usage.
Basically, EARLIER function will give you access to values of different row context.
When you use CALCULATE function it creates a row context of the whole table, theoretically it iterates over every table row. The same happens when you use FILTER function it will iterate on the whole table and evaluate every row against the filter condition.
So far we have two row contexts, the row context created by CALCULATE and the row context created by FILTER. Note FILTER use the EARLIER to get access to the CALCULATE's row context. Having said that, in our case for every row in the outer (CALCULATE's row context) the FILTER returns a set of rows that correspond to the current id in the outer context.
If you have a programming background it could give you some sense. It is similar to a nested loop.
Hope this Python code points the main idea behind this:
outer_context = ['row1','row2','row3','row4']
inner_context = ['row1','row2','row3','row4']
for outer_row in outer_context:
for inner_row in inner_context:
if inner_row == outer_row: #this line is what the FILTER and EARLIER do
#Calculate the min datetime using the filtered rows
...
...
UPDATE 2: Adding a ranking column.
To get the desired rank you can use this expression:
RankColumn =
RANKX(
CALCULATETABLE(Table,ALLEXCEPT(Table,Table[id]))
,Table[datetime]
,Hoja1[datetime]
,1
)
This is the table with the rank column:
Let me know if this helps.

how to access one by on value of the stored procedure and calculate the average of them row wise

I executed a stored procedure in java which results into a table of 16 column and 3600 rows.Now I want to access 3 column of the retrieved table in row by row fashion and calculate the average of all column in row wise fashion.To do this,I need to access values of columns one by one .How to do it. My code for accessing column is
while (rs.next())
{
a1=rs.getDouble(7);
a2=rs.getDouble(8);
a3=rs.getDouble(10);
sum +=(a1+a2+a3);
}
return sum/3;
but through this,i think i'am getting a total average of all the columns .But i need average of column row by row i.e average of all three column of first row,then second row ...like this.
You are indeed adding all the values from 3 columns and doing row by row addition.
If you want row by row, then for each entry that you get from database you need to store the result in a list like below and return it where needed:
List<Double> averageList = new ArrayList<Double>();
while (rs.next())
{
a1=rs.getDouble(7);
a2=rs.getDouble(8);
a3=rs.getDouble(10);
averageList.add((a1+a2+a3)/3.0);
}
return averageList;