Subscripted assignment dimension mismatch.

5 次查看(过去 30 天)
Hey guys,
I'm pretty new to Matlab so please bear with me :)
I wrote the following code
============================================================================
Result2 = zeros(1, 324012);
for i = 1:324012
if (Result1(1, i) < 0)
Result2(1, i) = dec2bin(typecast(int16(Result1(1,i)),'uint16'));
else
Result2(1, i) = dec2bin(Result1(1,i));
end
end
============================================================================
And I get the following error
============================================================================
??? Subscripted assignment dimension mismatch.
Error in ==> Untitled4 at 11 Result2(1, i) = dec2bin(Result1(1,i));
============================================================================
What am I doing wrong?
Thanks a lot.
Assaf.

回答(6 个)

Matt J
Matt J 2012-10-31
编辑:Matt J 2012-10-31
The output of DEC2BIN is non-scalar, yet you are trying to assign its output to a scalar location Result2(1, i).
What are you trying to accomplish with this code?

Wayne King
Wayne King 2012-10-31
编辑:Wayne King 2012-10-31
Because the output of dec2bin() is a char array of a certain number of elements. You do not tell us what Result1 is, but suppose you just had
Result1 = randi([-4 4],3,1);
If I now try to do:
Result1(1) = dec2bin(3);
that will produce the error you see, because I'm trying to assign to one element of Result1 the 1x2 character array: 11
What you can do is to make Result1 a cell array. As an example, I'll just initialize Result1 as a cell array the same dimension as Result2 and just put a single randomly chosen integer from [-4 4] in each element.
Result2 = zeros(1,324012);
Result1 = cell(size(Result2));
[Result1{:}] = deal(randi([-3,3],1,1));
for ii = 1:324012
if (Result1{ii} < 0)
Result2(ii) = dec2bin(typecast(int16(Result1{ii}),'uint16'));
else
Result2(ii) = dec2bin(Result1{ii});
end
end

assafmalki Malki
assafmalki Malki 2012-10-31
编辑:assafmalki Malki 2012-10-31
Hey,
Thanks a lot for the help.
Here is the entire code
============================================================================
for Vd=150:2:1000
n = ceil(2*pi/(A*Vd));
for k=1:n
j = j+1;
Result(j) = cos(A*Vd*k);
end
end
Result1 = Result * 2^15;
Result2 = zeros(1, 324012);
for i = 1:324012
if (Result1(1, i) < 0)
Result2(1, i) = eval(dec2bin(typecast(int16(Result1(1,i)),'uint16')));
else
Result2(1, i) = eval(dec2bin(Result1(1,i)));
end
end
============================================================================
As you can see result1 is already a 1x324012 double vector so I don't think I can initialize it as a cell array.
Thanks a lot guys.
Assaf.
  2 个评论
Matt J
Matt J 2012-10-31
This new version of the code shouldn't produce any errors, now that you're using EVAL. What do you now consider the problem to be?
Daniyal Awan
Daniyal Awan 2012-10-31
编辑:Daniyal Awan 2012-10-31
you could still use something like mat2cell() (with proper inputs) to convert your vector into cell-vector of same dimensions. Even though i think using 'eval' is also fine, do you still get error ?

请先登录,再进行评论。


Matt J
Matt J 2012-10-31
编辑:Matt J 2012-10-31
I'm still not completely sure what you intend to do with this code, but my guess is that your entire code could be replaced by the following 1 line
Result2=dec2bin(uint16(Result1));
  1 个评论
assafmalki Malki
assafmalki Malki 2012-10-31
It didn't help, because it doesn't use 2 complement for the negative numbers, just replacing them with a zero...
Thanks.

请先登录,再进行评论。


assafmalki Malki
assafmalki Malki 2012-10-31
Hey,
Thanks again.
So this is what I'm trying to do:
I have a vector 1x324012 double named Result, I want a binary representation of this vector. Result is a product of the cosine function so obviously the binary representation of almost all the elements in the vector (except 1) will be zero, that's way I multiply it by 2^15 and try to find the binary representation of that. I also need it to be in 2 complement because cosine can produce negative numbers and also to be outputted to a text file.
If you can tell me a better way to do so, it will be much appreciated.
Thanks a lot.
Assaf.
  3 个评论
assafmalki Malki
assafmalki Malki 2012-10-31
That's way I multiply by 2^15 at the beginning so I wont have numbers such as (0.2323...) for it the to truncate. I need the 2 complement for those numbers (after the multiplication).
Thanks a lot.
Assaf.

请先登录,再进行评论。


Matt J
Matt J 2012-10-31
编辑:Matt J 2012-10-31
This converts Result1 to a bit string array containing its two's complement
Result1=[-3 -2 -1 0 1 2 3];
neg=Result1<0;
tc=bitcmp(uint16(-Result1(neg)))+1;
Result2=uint16(Result1);
Result2(neg)=tc;
Result2=dec2bin(Result2),
The output of the above will be
Result2 =
1111111111111101
1111111111111110
1111111111111111
0000000000000000
0000000000000001
0000000000000010
0000000000000011
  2 个评论
assafmalki Malki
assafmalki Malki 2012-10-31
Hey,
Looking good, I have one problem with that due, I get as a result one matrix 324012x16 char, I cant even display that, it gives me the following:
Cannot display summaries of variables with more than 524288 elements.
How can I convert it to 324012x1 String?
An another thing for my own knowledge, when I write something like that
tc=bitcmp(uint16(-Result1(neg)))+1;
It only takes into consideration the elements which has '1' in the neg vector?
Thanks a lot.
Assaf.
Matt J
Matt J 2012-10-31
编辑:Matt J 2012-10-31
Looking good, I have one problem with that due, I get as a result one matrix 324012x16 char, I cant even display that, it gives me the following: Cannot display summaries of variables with more than 524288 elements.
I don't recognize that error message. Are you using the Student Version of MATLAB? In any case, you probably wouldn't want to display such a huge thing anyway. Didn't you plan to save it to a file? You can display some chunk of it by doing things like,
Result2(1:10,:)
How can I convert it to 324012x1 String?
If you want it as a string, you can't make it 324012x1. Each number needs 16 bits and therefore 16 characters.
It only takes into consideration the elements which has '1' in the neg vector?
The "neg" vector was generated as follows neg=(Result1<0); and therefore Result1(neg) will consist only of negative values in Result1. Similarly Result2(neg)=something will only consider elements in those same positions.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by