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.
0 个评论
回答(6 个)
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
0 个评论
assafmalki Malki
2012-10-31
编辑:assafmalki Malki
2012-10-31
2 个评论
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
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
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 个评论
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 Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!