MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

Help with a piece of code

jip prinsen さんによって質問されました 2017 年 9 月 21 日
最新アクティビティ José-Luis
さんによって コメントされました 2017 年 9 月 21 日
Hey all, I'm working on a school project that's about changing the order of characters in a binary array. In the process of writing it, I found a bit of code online to help me:
v = sprintf('%d', array3)
endarray = v - '0'
Basically, what this does is it takes the value of array3, which for example is [1011010] and creates a variable endarray, which in turn becomes [1 0 1 1 0 1 0]. When I implemented this piece of code I didn't quite know how it functioned, and it wasn't explained on the page where I found it, so I decided to go back to it later. Unfortunately I still haven't figured it out. I can't wrap my head around the combinations of sprintf, %d, and the -'0'.
Any help with understanding these two lines would be greatly appreciated.

  1 件のコメント

Pal Szabo
2017 年 9 月 21 日
I recommend to replace "Help with a piece of code" with something more meaningful

サインイン to comment.

2 件の回答

Pal Szabo
回答者: Pal Szabo
2017 年 9 月 21 日
編集済み: Pal Szabo
2017 年 9 月 21 日
 採用された回答

array3 is an integer. Sprintf needs to no that it is in decimal point notation, that's why you need the %d. More info: https://uk.mathworks.com/help/matlab/matlab_prog/formatting-strings.html. Strings in matlab are basically chain of chars. By subtractring -'0', you are basically subtracting 0 (decimal notation) from the ASCII values of the chars within the string. Executing an operation on each char within the chain of chars (aka on the string) gives a result of individual chars, since the operation only valid on individual chars, not on the string as a whole. Try this for example:
array3 = [1011010]
v = sprintf('%d', array3)
endarray = v - '0' - 'd'
You get
-99 -100 -99 -99 -100 -99 -100
as result. 'd' is the ASCII character with decimal value 100. https://en.wikipedia.org/wiki/ASCII
You get the same result if define endarray like this:
endarray = v - '0' - 100
Here 100 is an integer, not a chain of chars.
Sub in different values for 'd': the results are consistent with the ASCII table.
Hope this helps.
EDIT: I see you added a tag 'binary'. I do not think array3 is binary, just because it has only ones and zeros, it is pretty much a decimal value. Just like 8+2=10, 10 is not binary.

  3 件のコメント

José-Luis
2017 年 9 月 21 日
array3 is an integer. Sprintf needs to no that it is in decimal point notation, that's why you need the %d.
How do you figure that?
sprintf('%d',0.5)
Pal Szabo
2017 年 9 月 21 日
Okay you are right, I may be confusing the meaning of the expressions "decimal point" and "base 10". If I say
array3 is an integer. Sprintf needs to no that it is a number in base 10, that's why you need the %d.
Is this right?
José-Luis
2017 年 9 月 21 日
Looks like array3 is a scalar and not a logical array.

サインイン to comment.


José-Luis
回答者: José-Luis
2017 年 9 月 21 日
編集済み: José-Luis
2017 年 9 月 21 日

Generating a "binary" (logical) array:
a = rand(10,1) > 0.5;
Transforming the binary array into a string. v is a character array.
v = sprintf('%d', a)
Transforming the string into a numeric array:
endarray = v - '0'
Matlab does implicit conversions. When you do arithmetic operation with characters, it transforms them into double and then performs the operations.
'0' - 48
Please note that all that is unnecessary if you want an array of doubles from a logical array. You could just have:
endarray = double(a)

  2 件のコメント

Guillaume
2017 年 9 月 21 日
The starting point is not a logical array but a binary number encoded as a decimal, e.g.
a = 1011010;
Hence the trip through a char array to get the digits of that decimal number.
José-Luis
2017 年 9 月 21 日
Thanks. I was wrong but in essence sort of right.
Definitely maybe.

サインイン to comment.



Translated by