필터 지우기
필터 지우기

getting very weird number from hex2dec()

조회 수: 2 (최근 30일)
William
William 2012년 1월 20일
Hello,
I am attempting to convert large matrices of doubles to hex. I have an S matrix [2048 x 984] full of zeros
If I do this
>> dec2hex(s1a(1,3))
ans =
0
which is what should happen. However If I do this.....
HexS1a = ones(2048,984);
for aa = 1:984
for bb = 1:2048
HexS1a(bb,aa) = HexS1a(bb,aa)*dec2hex(s1a(bb,aa));
end
end
I get a matrix of "48"'s not zeros. The type is double. Can anyone explain this? It's driving me nuts!

채택된 답변

Walter Roberson
Walter Roberson 2012년 1월 20일
dec2hex(sla(1,3)) is not producing a digit zero: it is producing the string '0' . The numeric code for the character '0' happens to be 48. char(48) = '0'
ones(2048,984) is a double precision datatype. When you store a character to a double precision data type, the character gets converted to its numeric code.
If you are planning to multiply hex digits then you will need something more sophisticated that the "*" operator.

추가 답변 (1개)

William
William 2012년 1월 20일
how would I go about rigging it so I just get zero?
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 1월 20일
t = dec2hex(sla(1,3)) - '0';
t(t>9) = t(t>9) - 7;
However, if this is your aim, to get the numeric equivalent of each group of 4 bits, you can do that in other ways. For example,
HexPerVal = 2; %hex digits per sla entry
t2 = reshape(dec2bin(sla(:),HexPerVal*4).', 4, []).' - '0';
t3 = reshape(t2(:,1) * 8 + t2(:,2) * 4 + t2(:,3) * 2 + t2(:,4),2,[]).';
t4 = permute(reshape(t3,[HexPerVal,3,5]),[2 3 1]);
Now t4 would be an array whose first two dimensions were the same as sla's dimensions, and whose third dimension would be 1 to the number of hex digits per value (as determined by HexPerval)

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

카테고리

Help CenterFile Exchange에서 Digital Input and Output에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by