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

5 views (last 30 days)
Commented: dpb 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(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.

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

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 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.

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 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.