Subscripted assignment dimension mismatch.
古いコメントを表示
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 件)
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
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
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 ?
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));
assafmalki Malki
2012 年 10 月 31 日
0 投票
3 件のコメント
Please use Comments to respond to people's Answers and suggestions (like I'm doing now), instead of responding with Answers of your own. This allows readers to distinguish between Answers and non-Answers.
Other than that, have you tried the solutions that people have suggested to you? As far as we know, we've already given you exactly what you want.
Finally, the code you showed us initially is truncating all negative numbers to 0 (by converting them to uint16), so it is not clear why you now say you need 2-s complement.
assafmalki Malki
2012 年 10 月 31 日
Matt J
2012 年 10 月 31 日
See my latest Answer.
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
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.
カテゴリ
ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!