How to create a signal matrix

12 次查看(过去 30 天)
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.

采纳的回答

David Barry
David Barry 2016-12-5
编辑:David Barry 2016-12-5
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 个评论
David Barry
David Barry 2016-12-6
编辑:David Barry 2016-12-6
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)
Tommaso Fornaciari
Tommaso Fornaciari 2016-12-7
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.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by