Why are all the negative values in the matrix show as 0?

조회 수: 5 (최근 30일)
JCH
JCH 2023년 2월 8일
댓글: Steven Lord 2023년 2월 8일
The code below creates a matrix to do calculations, however, the negative values results are show in the matrix as 0. Why would this happen and how to keep those negative values instead of 0. Thank you.
load Foveal
T = ans;
T_1002 = T(matches(T.message,'1002'),:);
T_ENDSACC = T(matches(T.message,'ENDSACC'),:);
[x1 ~] = size(T_ENDSACC);
[x2 ~] = size(T_1002);
T_diff = zeros(x1,x2);
for q = 1:x1
T_diff(q,:) = T_ENDSACC.reltime(q) - T_1002.reltime(:);
end

채택된 답변

Voss
Voss 2023년 2월 8일
reltime is of class uint32, as shown here:
load Foveal
T = ans;
class(T.reltime)
ans = 'uint32'
An unsigned integer cannot be negative. If you want to allow negative values use a different type such as a signed integer or a floating-point (such as single or double). Here I'll make reltime into a double:
T.reltime = double(T.reltime);
And then run the rest of the code:
T_1002 = T(matches(T.message,'1002'),:);
T_ENDSACC = T(matches(T.message,'ENDSACC'),:);
[x1 ~] = size(T_ENDSACC);
[x2 ~] = size(T_1002);
T_diff = zeros(x1,x2);
for q = 1:x1
T_diff(q,:) = T_ENDSACC.reltime(q) - T_1002.reltime(:);
end
T_diff
T_diff = 57×5
-870 -3140 -5422 -6504 -7398 -586 -2856 -5138 -6220 -7114 -361 -2631 -4913 -5995 -6889 -164 -2434 -4716 -5798 -6692 90 -2180 -4462 -5544 -6438 337 -1933 -4215 -5297 -6191 545 -1725 -4007 -5089 -5983 1171 -1099 -3381 -4463 -5357 1353 -917 -3199 -4281 -5175 1836 -434 -2716 -3798 -4692
  댓글 수: 3
Voss
Voss 2023년 2월 8일
You're welcome!
Steven Lord
Steven Lord 2023년 2월 8일
FYI for the integer types you can use intmin and intmax to determine the range of allowed values for that type.
intmin('uint32')
ans = uint32 0
intmax('uint32')
ans = uint32 4294967295
Assignment of values outside that range truncates.
x = ones(1, 2, 'uint32')
x = 1×2
1 1
x(1) = -1 % -1 is < intmin('uint32') so MATLAB stores intmin('uint32')
x = 1×2
0 1
x(2) = 1e11 % 1e11 is > intmax('uint32') so MATLAB stores intmax('uint32')
x = 1×2
0 4294967295

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by