How do I make a code that will count the number of times the sign changes succesively through an array?

42 views (last 30 days)
Lana Sharp
Lana Sharp on 7 Sep 2015
Edited: Hamoon on 8 Sep 2015
I need to make a code that reads this array and counts the number of times there is a sign change. This is the array DAX = 0.0022 0.0085 -0.0178 -0.0117 -0.0199 -0.0135 -0.0177 -0.0474 -0.0196 0.0068 -0.0115 -0.0178 0.0050 -0.0098 -0.0096 0.0348 -0.0008 0.0442 -0.0203 -0.0205 -0.0156 0.0240 0.0266 0.0039 0.0293 0.0048 -0.0352 0.0054 -0.0274 0.0013 -0.0456 0 -0.0028 0.0024 -0.0487 -0.0197 -0.0104 -0.0128 0.0248 -0.0254 -0.0354 -0.0052 0.0528 -0.0515 -0.0079 0.0070 0.0515 0.0070 0.0107 -0.0007 0.0227 -0.0141 0.0021 0.0117 0.0062 0.0261 0.0026 0.0085 0.0085 -0.0132 -0.0523 0.0237 0.0113 0.0590 0.0007 -0.0081 -0.0063 0.0082 0.0301 0 0 0.0146 -0.0016 0.0130 0.0145 -0.0416 0.0034 0.0204 0.0172 0.0042 -0.0186 0.0209 0.0137 0.0275 -0.0101 0.0057 -0.0158 0.0226 -0.0096 -0.0026 -0.0264 0.0023 -0.0002 0.0239 0.0378 -0.0278 0.0037 -0.0001 0.0136 0.0030 -0.0137 0.0016 0.0400 0.0003 -0.0176 0.0020 0.0024 -0.0143 -0.0014 0.0106 0.0110 -0.0075 -0.0360 0.0002 -0.0187 0.0078 0.0004 -0.0306 0.0029 0.0270 -0.0074 -0.0050 0.0225 -0.0158 0.0199 -0.0389 -0.0022 -0.0120 -0.0116 -0.0056 0.0125 -0.0117 0.0314 0.0125 0.0302 0.0058 0.0043 0.0103 0.0126 0.0054 0.0243 -0.0034 0.0042 -0.0147 0.0183 0.0170 -0.0053 0.0176 -0.0018 -0.0119 0.0032 0.0164 -0.0075 -0.0247 0.0121 0.0095 -0.0172 -0.0205 0.0094 -0.0036 0.0150 0.0282 0.0104 0.0067 -0.0063 -0.0094 0.0086 -0.0108 -0.0242 -0.0014 -0.0035 0.0155 0.0146 0.0033 0.0167 0.0037 0.0052 -0.0007 0.0016 0.0126 0.0054 -0.0048 -0.0003 -0.0171 -0.0043 0.0274 -0.0040 -0.0067 -0.0215 -0.0157 0.0075 0.0267 -0.0030 0.0133 -0.0008 0.0124 -0.0120 0.0242 -0.0040 -0.0151 0.0188 -0.0070 0.0037 -0.0122 -0.0039 -0.0172 -0.0013 -0.0249 -0.0149 0.0029 -0.0144 0.0168 0.0067 0.0013 0.0237 -0.0012 0.0098 -0.0008 0.0040 0.0205 -0.0046 0.0016 -0.0149 -0.0069 0.0241 -0.0056 0.0058 -0.0046 0.0009 -0.0020 0.0082 -0.0057 -0.0168 -0.0072 0.0108 0.0082 0.0080 0.0016 0.0157 -0.0100 -0.0023 0.0169 0.0026 0.0020 0.0076 0 0 0.0014 -0.0090 0.0151 0 -0.0027 0.0004 -0.0025 0.0030 0.0005 -0.0163 0.0034 0.0043 -0.0190 0.0072 0.0097 -0.0211 -0.0180 -0.0090 -0.0113 0.0066 -0.0045 -0.0184 0.0123 0.0081 0.0097 -0.0066 -0.0248 -0.0180 0.0093 0.0024 0.0069 -0.0059 -0.0006 0.0019 0.0146 0.0100 0.0057 0.0073 -0.0059 -0.0149 0.0020 -0.0149 0.0119 0.0203 0.0110 0.0071 -0.0039 0.0141 -0.0002 0.0017 0.0086 -0.0014 0.0028 -0.0070 0.0114 0.0089 -0.0020 -0.0050 0.0008 0.0050 0.0036 0.0154 -0.0021 0.0060 -0.0023 0.0018 0.0132 0 0 0.0027 -0.0048 -0.0082 0.0125 0.0002 -0.0032 0.0076 0.0021 -0.0177 -0.0030 0.0164 -0.0054 -0.0099 0.0261 -0.0276 -0.0123 0.0099 -0.0015 0.0051 -0.0263 -0.0081 -0.0085 -0.0332 0.0516 0.0033 0.0239 0.0110 -0.0317 0.0017 0.0146 -0.0479 -0.0066 -0.0041 -0.0236 0.0154 0.0306 0.0015 0.0030 0.0028 -0.0000 0.0122 -0.0193 -0.0057 -0.0062 0.0196 0.0119 -0.0014 0.0127 0.0081 0.0026 0.0053 -0.0011 0.0121 -0.0038 -0.0103 -0.0145 -0.0074 0.0142 -0.0339 0.0023 -0.0183 -0.0040 -0.0031 0.0212 0.0087 0.0071 0.0049 0.0020 0.0186 0.0030 -0.0098 -0.0179 -0.0052 -0.0070 0.0038 0.0250 0.0039 0.0045 0.0021 -0.0046 -0.0072 0.0022 0.0232 0.0025 0.0037 0.0004 -0.0117 0.0146 -0.0103 -0.0213 -0.0031 -0.0040 0.0000 0.0156 -0.0032 -0.0181 -0.0116 0.0010 -0.0126 -0.0061 0.0022 0.0065 -0.0044 0.0264 -0.0000 0.0083 0.0033 -0.0061 0.0076 0.0157 0.0022 -0.0022 -0.0020 -0.0064 0.0136 -0.0030 -0.0108 -0.0038 0.0182 -0.0031 -0.0004 -0.0047 -0.0029 -0.0028 -0.0125 0.0132 0.0088 0.0009 0.0025 0.0028 -0.0008 0.0204 0.0032 0.0057 0.0037 -0.0040 0.0052 0.0132 -0.0008 0.0050 -0.0038 -0.0069 0.0056 0.0075 -0.0055 0.0175 0.0029 -0.0005 0.0055 -0.0101 0.0005 0.0017 0.0082 0.0047 -0.0173 0.0176 0.0082 -0.0045 -0.0223 -0.0014 0.0263 0.0132 -0.0014 0.0010 0.0068 -0.0037 -0.0017 0.0060 0.0033 -0.0003 -0.0016 0.0011 -0.0060 0.0052 0.0084 -0.0014 -0.0014 -0.0124 0 0.0002 0.0033 -0.0117 0 0.0109 -0.0021 -0.0051 0.0060 -0.0048 -0.0132 0.0122 0.0182 0.0009 0.0001 0.0003 0.0092 -0.0085 -0.0083 0.0054 0.0008 -0.0012 0.0096 0.0040 -0.0074 -0.0036 0.0150 -0.0001 0.0014 0.0031 0.0093 0.0054 -0.0003 0.0026 0.0042 0.0034 0.0005 0.0019 -0.0012 0.0029 -0.0142 -0.0005
I know that I probably need to make a loop I'm just not sure how to do this. I could easily count the number of positive and negative numbers by sorting positive = DAX(DAX>0) etc, but thats not what I'm looking for. Please help me compute the number of times that this array changes its sign in successive days.

Accepted Answer

Hamoon
Hamoon on 7 Sep 2015
pos = DAX>0;
changes = xor(pos(1:end-1),pos(2:end));
num = sum(changes)
  2 Comments
Hamoon
Hamoon on 7 Sep 2015
The second line compute binary xor operator (Exclusive or) for each two adjacent elements , and xor acts as follows:
xor(1,1)=0
xor(0,0)=0
xor(1,0)=1
xor(0,1)=1
but you still need to decide what you want to do with a zero in DAX array. in this code, zeros are considered to act like negatives because we simply said positives are those which are greater than zero (pos = DAX>0) so others are negatives.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!