Castielle
Castielle

Reputation: 435

Rank values in a dataset row

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?

Upvotes: 2

Views: 484

Answers (1)

Jonas
Jonas

Reputation: 74930

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);

Upvotes: 1

Related Questions