How to create a signal matrix

Hello, I am trying to replicate a matrix (signals2) that gives me signals of 1,0 or 1 depending on the value of each single observation in another matrix (rankfull2)-the two have the same size. I would like matrix "signals2" to show 1 when the value is above 319, -1 when is below 60 and 0 otherwise.
for iii=1:size(rankfull2,1)
for jjj= 1:size(rankfull2(1,:),2)
if rankfull2(iii,jjj) < 60
signals2(iii,jjj)= -1;
elseif rankfull2(1,jjj) > 319
signals2(iii,jjj)= 1;
else signals2(iii,jjj)= 0;
end
end
end
The output that I get seems to understand the rule of lower than 60 but not the one above 319, returning a 0 instead of a 1 (each row contains at leat 50 values above 319)
Could you someone advise if there is something wrong with my loop or if I am unaware of certain matlab rules?
Thank you in advance.

 Accepted Answer

David Barry
David Barry on 5 Dec 2016
Edited: David Barry on 5 Dec 2016
It looks like the issue might be because you are only looking in the first row of rankfull2 with your >319 line of code. Anyway, there is a much cleaner way of doing this without the need for loops. You should take a look at logical indexing.
signals2 = zeros(size(rankfull2));
signals2(rankfull2 < 60) = -1;
signals2(rankfull2 > 319) = 1;

3 Comments

Great, I am only now getting to know matlab. In my rankfull2 matrix I have some NaNs that change from row to row as well. So I would like the decision criteria not to be fixed but dependent on the number of non NaNs that i have in each row. In other words, immagine the first rankfull2 row to have 600 observations of which 200 NaNs, I would like signals2 to show -1 when rankfull2 is below 40 and 1 when is above 560. The second row will have other criteria depending on the number of non NaNs in rankfull2 row 2 and so on. The criteria is -1 for values below 10% of non NaNs and 1 for values above 90% of non NaNs, allowing me to have the same number of 1s and -1s. Hope I was clear enought.Can I easily adjust the code you provided me with my situation?
I don't completely follow what you need to do but it sounds like you might need to make use of the insan function along with the logical indexing examples I provided. You could also use the sum function to count the number of NaN in each column if that is what you need.
Take a look at what this example produces:
a = [1, 2, NaN, 4; NaN, 6, 7, 8; NaN, 10, 11, 12];
idx = isnan(a)
sum(idx)
Thank you, I have tried to figure it out but I still have problems. I have a matrix C that counts the non NaNs of A per row.
A
% 1 7 -2 9 4 18 11 NaN -5 10
NaN 8 -3 4 22 -58 NaN NaN 0 3
6 8 -1 3 NaN 99 2 -50 NaN 43
C
9
7
8
The desired output matrix B would be:
Row1: -1 if lower than 0.1*9 and 1 if bigger than(1-0.1)*9 Row2: -1 if lower than 0.1*7 and 1 if bigger than (1-0.1)*7 Row 3: -1 if lower than 0.1* 8 and 1 if bigger than (1-0.1)*8
Hope you understood. Would greatly appreciate your help again. Thank you.

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!