Getting this error "In an assignment A(:) = B, the number of elements in A and B must be the same."

1 view (last 30 days)
clear all;
bit=18; %bit for the sine sample
Sample=200; % for a 200 sample LUT
Phase=2*pi/Sample;
sine=zeros(1,Sample);
new=zeros(1,Sample);
Mul=double(131071);
for Kr=1:Sample
phi=(Kr)*Phase;
sine(Kr)=round(sin(phi)*Mul);
%convert the fp to hex
if sine(Kr)<0
new(Kr)=dec2hex(((2^bit)-sine(Kr)),5);
else
new(Kr)=dec2hex(sine(Kr),5);
end
end
Im keep getting error at line
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Sine_200_sample (line 22)
new(Kr)=double(dec2hex(sine(Kr),5));
Basically the new array has the same row and collum as the "sine" array , i just wanted to move the converted decimal to hex to the "new" array. But im not quite sure how array indexing are handled in matlab.

Accepted Answer

Star Strider
Star Strider on 24 Dec 2019
The problem is that a 5-element character array is not going to fit in a scalar element of ‘new’.
A cell array is the easiest solution:
bit=18; %bit for the sine sample
Sample=200; % for a 200 sample LUT
Phase=2*pi/Sample;
sine=zeros(1,Sample);
new=cell(1,Sample);
Mul=double(131071);
for Kr=1:Sample
phi=(Kr)*Phase;
sine(Kr)=round(sin(phi)*Mul);
%convert the fp to hex
if sine(Kr)<0
new{Kr}=dec2hex(((2^bit)-sine(Kr)),5);
else
new{Kr}=dec2hex(sine(Kr),5);
end
end
  2 Comments
Mohamad Hafiz Mohamad Rodzi
Thanks for the reply strider,
Your answer apparently help me to realize that the "new" array cannot hold the data types of the returned dec2hex results if that what you mean by "character array is not going to fit in a scalar element of ‘new' ’"
Star Strider
Star Strider on 24 Dec 2019
As always, my pleasure!
Your answer apparently help me to realize that the "new" array cannot hold the data types of the returned dec2hex results if that what you mean by "character array is not going to fit in a scalar element of ‘new' ’"
It is.

Sign in to comment.

More Answers (2)

David Hill
David Hill on 24 Dec 2019
clear all;
bit=18; %bit for the sine sample
Sample=200; % for a 200 sample LUT
Phase=2*pi/Sample;
sine=zeros(1,Sample);
new=zeros(Sample,5);%dec2hex(x,5) generates 5 numbers
Mul=double(131071);
for Kr=1:Sample
phi=(Kr)*Phase;
sine(Kr)=round(sin(phi)*Mul);
%convert the fp to hex
if sine(Kr)<0
new(Kr,:)=dec2hex(((2^bit)-sine(Kr)),5);
else
new(Kr,:)=dec2hex(sine(Kr),5);
end
end
See above, to correct your problem.

dpb
dpb on 24 Dec 2019
dec2hex returns the character representation of the value in its argument in hexadecimal...that will be in your case an array of length 5 as you asked for specifically that many digits,
Hence, the cast to double returns an array of each of those characters in numeric representation, NOT the conversion of the hex string to a double number as you probably are expecting. The assignment of that array to a single element in your array is the proximate cause of the error message.
>> (dec2hex(round(0.5*131071),5))
ans =
'10000'
>> double(ans)
ans =
49 48 48 48 48
>> hex2num(dec2hex(round(0.5*131071),5))
ans =
1.2882e-231
>> hex2dec(dec2hex(round(0.5*131071),5))
ans =
65536
>>
NB: that the conversion to double is still probably not what you're looking for...that's the representation of the bit pattern as a double, not the decimal integer of the hex string.
Depending upon your end goal, many or most of these transformations may not be needed...but you don't describe that.
  2 Comments
Mohamad Hafiz Mohamad Rodzi
Hi dpb,
My goal is to have the hex representation of the argument values. I wonder if the character representation that i've been trying to place in the "new" array is which only accept numeric representation that cause the error.
dpb
dpb on 24 Dec 2019
Yes, by trying to convert the hex representation you have to a double(), you've turned into an array instead of an element. But, if you'll look at the above, that's not what you want anyway, the result of that is the values of the chracters in the hex string as numbers, not characters--that's what the values 48, 49 are--
You already have the hex representation after you do the dec2hex() operation--it's a char() array string. To store that as a single element in an array, use cellstr() or {} or convert to the new string representation.
But, again, none of that may be necessary for anything other than viewing depending upon just what is your objective here....the numeric value internally is the same regardless how you display it.

Sign in to comment.

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!