How to loop this over the columns?

2 views (last 30 days)
Hello, I currently have a code that deals with some indicator and signaling as follows for one row of data indexed by the variable idx. The code is as follows:
syms = ['A' 'B' 'C' 'D' 'TU']; %%Matrix of size (1xN) containing Strings
tday = rand(2000,1); %%Matrix that I want idx to pull values out of which corresponds to the string matched
cl = rand(2000, 5) %%%%Another Matrix that I want idx to pull values out of which corresponds to the string matched
idx=strmatch('TU', syms, 'exact');
tday=tday(:, idx);
cl = cl(:,idx);
lookback=30
holddays=30;
longs=cl > backshift(lookback, cl) ; %%For Backshift Function Please see Function Defined Below.
shorts=cl < backshift(lookback, cl) ;
pos=zeros(length(cl), 1);
for h=0:holddays-1
long_lag=backshift(h, longs);
long_lag(isnan(long_lag))=false;
long_lag=logical(long_lag);
short_lag=backshift(h, shorts);
short_lag(isnan(short_lag))=false;
short_lag=logical(short_lag);
pos(long_lag)=pos(long_lag)+1;
pos(short_lag)=pos(short_lag)-1;
end
ret=(backshift(1, pos).*(cl-backshift(1, cl))./backshift(1, cl))/holddays;
ret(isnan(ret))=0;
idx=find(tday==20070103);
% idx=1;
cumret=cumprod(1+ret(idx:end))-1;
plot(cumret);
This code currently works for pulling one matrix out... How would I do it if I want to iterate over all the columns for all the strings contained in syms? What would be the most efficient way to get this done and would there be a way to not specify any string matches instead since I want to do it for all the symbols contained in the matrix strings ? I know I am supposed to do a nested loop but it says that there was an issue with my dimension (I think it may have to do with the fact that h is indexed at 0, which I am also kind of confused about.
As a side note, the function above defined as backshift has the following code:
function y=backshift(day,x)
% y=backshift(day,x)
assert(day>=0);
y=[NaN(day,size(x,2), size(x, 3));x(1:end-day,:, :)];
end
Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 27 Jan 2016
Your line
syms = ['A' 'B' 'C' 'D' 'TU']
creates
syms = 'ABCDTU'
You need a cell array of strings,
syms = {'A' 'B' 'C' 'D' 'TU'}
  1 Comment
Putsandcalls
Putsandcalls on 27 Jan 2016
Edited: Putsandcalls on 27 Jan 2016
Oops sorry about that, I meant that it was a cell array when I was creating the example, but what would be your recommended advice ? Im a bit unsure about why the indexing is 0, I can just replace it with 1 and do a nested loop over the columns and removing the IDX right ?
Thanks

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!