How can I get average of the multidimensional matrix datas in Julia? - julia
Can I get the average of multidimensional matrix data in the same place in Julia?
Such as:
A=[
2 4 6
8 10 12
]
B=[
4 6 8
10 12 14
]
Average=[
3 5 7
9 11 13
]
If I have a matrix file, can I get the average of all the data in the same place in the matrix file?
In the same row and column?
And also, I cannot use any statistics method in my data. (using Statistics)
Here is my code.
julia> using BioStructures
julia> struc0=read("ranked_0.pdb",PDB)
ProteinStructure ranked_0.pdb with 1 models, 1 chains (A), 398 residues, 6267 atoms
julia> struc1=read("ranked_1.pdb",PDB)
ProteinStructure ranked_1.pdb with 1 models, 1 chains (A), 398 residues, 6267 atoms
#PDB is protein structure file
#calphaselector collects alpha-carbon in protein structure
julia> calphas1=collectatoms(struc1['A'],calphaselector)
398-element Vector{AbstractAtom}:
Atom CA with serial 5, coordinates [-48.92, 44.228, -7.122]
Atom CA with serial 22, coordinates [-45.297, 43.116, -7.895]
Atom CA with serial 44, coordinates [-44.734, 41.122, -11.106]
Atom CA with serial 68, coordinates [-43.19, 37.683, -11.734]
Atom CA with serial 90, coordinates [-40.072, 37.975, -13.951]
Atom CA with serial 109, coordinates [-39.962, 34.961, -16.27]
Atom CA with serial 120, coordinates [-36.574, 35.287, -18.039]
Atom CA with serial 142, coordinates [-36.609, 33.35, -21.312]
Atom CA with serial 152, coordinates [-34.149, 30.463, -21.787]
Atom CA with serial 171, coordinates [-32.469, 30.904, -25.188]
Atom CA with serial 190, coordinates [-32.407, 27.558, -27.034]
Atom CA with serial 201, coordinates [-28.96, 27.662, -28.654]
Atom CA with serial 211, coordinates [-28.549, 24.452, -30.698]
⋮
Atom CA with serial 6078, coordinates [-24.236, -1.604, 12.74]
Atom CA with serial 6093, coordinates [-26.218, 1.577, 11.906]
Atom CA with serial 6117, coordinates [-24.924, 4.783, 13.587]
Atom CA with serial 6136, coordinates [-27.904, 7.205, 13.764]
Atom CA with serial 6147, coordinates [-28.211, 10.904, 14.654]
Atom CA with serial 6154, coordinates [-25.655, 12.413, 17.087]
Atom CA with serial 6168, coordinates [-24.017, 9.206, 18.385]
Atom CA with serial 6179, coordinates [-20.62, 7.793, 19.378]
Atom CA with serial 6193, coordinates [-19.557, 4.234, 18.472]
Atom CA with serial 6214, coordinates [-16.767, 2.846, 20.702]
Atom CA with serial 6228, coordinates [-14.132, 0.846, 18.748]
Atom CA with serial 6247, coordinates [-11.916, 0.05, 21.82]
julia> calphas0=collectatoms(struc0['A'],calphaselector)
398-element Vector{AbstractAtom}:
Atom CA with serial 5, coordinates [-36.089, 66.106, 25.299]
Atom CA with serial 22, coordinates [-33.821, 64.881, 22.365]
Atom CA with serial 44, coordinates [-33.557, 62.444, 20.056]
Atom CA with serial 68, coordinates [-32.879, 58.931, 18.504]
Atom CA with serial 90, coordinates [-29.619, 58.508, 16.472]
Atom CA with serial 109, coordinates [-30.364, 57.074, 13.005]
Atom CA with serial 120, coordinates [-28.582, 54.105, 11.366]
Atom CA with serial 142, coordinates [-25.95, 54.934, 8.729]
Atom CA with serial 152, coordinates [-25.746, 52.003, 6.279]
Atom CA with serial 171, coordinates [-22.186, 52.037, 4.888]
Atom CA with serial 190, coordinates [-22.536, 50.445, 1.448]
Atom CA with serial 201, coordinates [-19.07, 48.955, 0.906]
Atom CA with serial 211, coordinates [-19.304, 47.721, -2.701]
⋮
Atom CA with serial 6078, coordinates [-30.501, 3.783, 9.927]
Atom CA with serial 6093, coordinates [-30.924, 7.529, 10.661]
Atom CA with serial 6117, coordinates [-28.57, 8.891, 13.374]
Atom CA with serial 6136, coordinates [-30.304, 12.0, 14.818]
Atom CA with serial 6147, coordinates [-29.161, 14.746, 17.23]
Atom CA with serial 6154, coordinates [-26.384, 13.957, 19.761]
Atom CA with serial 6168, coordinates [-26.4, 10.132, 19.458]
Atom CA with serial 6179, coordinates [-23.963, 7.201, 19.425]
Atom CA with serial 6193, coordinates [-24.374, 4.256, 17.037]
Atom CA with serial 6214, coordinates [-22.595, 1.092, 18.252]
Atom CA with serial 6228, coordinates [-20.864, -0.796, 15.39]
Atom CA with serial 6247, coordinates [-19.446, -3.617, 17.623]
# calculate Distance between alpha-carbon
function DistanceMap(el1::StructuralElementOrList,
el2::StructuralElementOrList)
dists = zeros(length(el1), length(el2))
for (i, subel1) in enumerate(el1)
for (j, subel2) in enumerate(el2)
dists[i, j] = distance(subel1, subel2)
end
end
return DistanceMap(dists)
end
function DistanceMap(el::StructuralElementOrList)
dists = zeros(length(el), length(el))
el_list = collect(el)
for i in 1:length(el)
for j in 1:(i - 1)
dist = distance(el_list[i], el_list[j])
dists[i, j] = dist
dists[j, i] = dist
end
end
return DistanceMap(dists)
end
julia> dm0=DistanceMap(calphas0)
Distance map of size (398, 398)
julia> dm1=DistanceMap(calphas1)
Distance map of size (398, 398)
#Here is some Distance Map Data
julia> dm0[1,3]
6.87824955929922
julia> dm1[1,1]
0.0
julia> dm1[1,3]
6.560646919321296
julia> println(typeof(size(dm0)))
Tuple{Int64, Int64}
julia>using Statistics
julia> mean(dm0,dims=1)
ERROR: MethodError: no method matching mean(::DistanceMap; dims=1)
Closest candidates are:
mean(::Any) at /builddir/build/BUILD/julia-1.6.5/build/usr/share/julia/stdlib/v1.6/Statistics/src/Statistics.jl:44 got unsupported keyword argument "dims"
mean(::Any, ::AbstractArray; dims) at /builddir/build/BUILD/julia-1.6.5/build/usr/share/julia/stdlib/v1.6/Statistics/src/Statistics.jl:104
mean(::Any, ::Any) at /builddir/build/BUILD/julia-1.6.5/build/usr/share/julia/stdlib/v1.6/Statistics/src/Statistics.jl:61 got unsupported keyword argument "dims"
...
Stacktrace:
[1] top-level scope
# REPL[19]:1
#I don't know the reason that the mean function doesn't work at all.
You can do this:
julia> foo = [
1 2 3
4 5 6
]
2×3 Matrix{Int64}:
1 2 3
4 5 6
julia> bar = [
8 2 7
9 6 3
]
2×3 Matrix{Int64}:
8 2 7
9 6 3
julia> (foo+bar)/2
2×3 Matrix{Float64}:
4.5 2.0 5.0
6.5 5.5 4.5
In your case, In the last phase of the DistanceMap function, you're returning the function object, which isn't what you want to do apparently. So the last line of the function should be replaced with the return dists:
function DistanceMap(el::StructuralElementOrList)
dists = zeros(length(el), length(el))
# do jobs...
return dists
end
function DistanceMap(el1::StructuralElementOrList, el2::StructuralElementOrList)
dists = zeros(length(el1), length(el2))
# do jobs...
return dists
end
Then, the (dm0+dm1)/2 will give you what you expect.
Using the mean function with specified dimension (specifically related to Statistics.jl package)
It seems to me you don't know how the mean(dm0,dims=1) works. Say I have this simple 2x3 matrix:
julia> foo = [
1 2 3
4 5 6
]
2×3 Matrix{Int64}:
1 2 3
4 5 6
We want to observe how the mean(foo, dims=1) works in this case. First, I should mention that performing the mean function on a collection of values calculates the mean of all the elements:
julia> mean(foo)
3.5
But what happens when we specify the dimension we want to take the mean? In this case, mean calculate the average value in the given matrix over a dimension. For example, If I want to take the mean of foo over the first dimension (each column), the following shows what is going to happen:
julia> mean(foo, dims=1)
1×3 Matrix{Float64}:
2.5 3.5 4.5
Related
What is the correct way to select rows from matrix by a boolean array?
I have a boolean array (from previous computations) and I would like to select the related rows from several matrices. That is why I need the proper index array (to be reused later). This is easy in Matlab and python but I do not crock the correct julian way of doing it... I am aware of DataFrames, but would like to find an orthodox matrix and array way of doing this. In Matlab I would say: n= 9; temp= 1:n; A= 1.0 + temp; someTest= mod(temp,2) == 0; % just a substitute of a more complex case % now I have both someTest and A! inds= find(someTest); Anew= A(inds,:); % I got inds (which I need)! What I have got working is this: n= 10; data= Array(1:n); A= 1.0 .+ data; someTest= rem.(data,2) .== 0; inds= [xy[2] for xy in zip(someTest,1:length(someTest)) if xy[1]]; # (*) Anew= A[inds,:]; What I assumed is that there is some shorter way to express the above phrase. in v. 0.6 there was find() function, but I have not gotten good sense of the julia documentation yet (I am a very very newbie in this).
You can use the BitArray just directly to select the elements: julia> A[someTest] 5-element Array{Float64,1}: 3.0 5.0 7.0 9.0 11.0 Fot your case: julia> A[someTest,:] == A[inds,:] true
find in 0.6 was renamed to findall in Julia 1.0. To get inds, you can simply do the following: inds = findall(someTest) You do not have to compute the intermediate someTest first, which would allocate an array you do not intend to use. Instead, you can do the test with findall directly passing a predicate function. inds = findall(x -> rem(x,2) == 0, data) This will return indices of data for which the predicate rem(x,2) == 0 returns true. This will not allocate an intermediate array to find the indices, and should be faster. As a side note, most of the time you do not need to materialize a range in Julia. Ranges are already iterable and indexable. They will automatically be converted to an Array when there is a need. Array(1:n) or collect(1:n) are usually redundant, and allocates more memory.
Your Matlab code doesn't work. A is just a row-vector (1x9 matrix), so when you try to do A(inds, :) you get an error: >> Anew= A(inds,:) Index in position 1 exceeds array bounds (must not exceed 1). But if you just fix that, you can solve the problem in exactly the same way in both Matlab and Julia, using either logical indices or regular ones: Matlab (I'm making sure it's a matrix this time): n = 9; temp = (1:n).'; A = temp * (1:4); inds = mod(temp,2) == 0; >> A(inds, :) % using logical indices ans = 2 4 6 8 4 8 12 16 6 12 18 24 8 16 24 32 >> A(find(inds), :) % using regular indices ans = 2 4 6 8 4 8 12 16 6 12 18 24 8 16 24 32 And now, Julia: n = 9; temp = 1:n; A = temp .* (1:4)'; # notice that we're transposing the opposite vector from Matlab inds = mod.(temp, 2) .== 0; # you can use iseven.(temp) instead julia> A[inds, :] # logical indices (BitArray) 4×4 Array{Int64,2}: 2 4 6 8 4 8 12 16 6 12 18 24 8 16 24 32 julia> A[findall(inds), :] # regular integer indices 4×4 Array{Int64,2}: 2 4 6 8 4 8 12 16 6 12 18 24 8 16 24 32 In this case, I would use the logical indices in both Julia and Matlab. In fact, the Matlab linter (in the editor) will tell that you should use logical indices here because it's faster. In Julia, however, there might be cases where it's more efficient to use inds = findall(iseven, temp), and just skip the logical BitArray, like #hckr says.
Can broadcast be applied to subarrays/slices of array in julia
I would like to broadcast to subarrays (i.e. broadcast to slices of array). This is useful in GPU programming for example I'd like to have: X,Y,Z = (rand(3,3,3) for _=1:3) #.[1,2] X = f(2X^2 + 6X^3 - sqrt(X)) + Y*Z where #.[1,2] means broadcasting along dim 3, i.e. apply colons to dim 1 and 2 in the expression. Is there a way to support this "sub-broadcast"? Edit: add an example julia> a = reshape(1:8, (2,2,2)) 2×2×2 Base.ReshapedArray{Int64,3,UnitRange{Int64},Tuple{}}: [:, :, 1] = 1 3 2 4 [:, :, 2] = 5 7 6 8 julia> broadcast(*, a, a) 2×2×2 Array{Int64,3}: [:, :, 1] = 1 9 4 16 [:, :, 2] = 25 49 36 64 julia> broadcast(*, a, a, dim=3) # I would like to broadcast the matrix multiplication (batch mode) instead of elementwise multiplication. 2×2×2 Array{Int64,3}: [:, :, 1] = 7 15 10 22 [:, :, 2] = 67 91 78 106 Edit 2: I am trying different vectorization methods here https://arrayfire.com/introduction-to-vectorization/ via the ArrayFire.jl package (a wrapper of arrayfire), including vectorization, parallel for-loops, batching, and advanced vectorizations. arrayfire has the gfor (http://arrayfire.org/docs/page_gfor.htm) method to run parallel computations on slices of matrices, and is implemented via broadcast in ArrayFire.jl. Currently, julia's broadcast acts element-wise. I just wonder if it can act "slice-wise" then it can do pure julia 3D and 4D support for Linear Algebra functions (https://github.com/arrayfire/arrayfire/issues/483). Of course normal nested for loops will get the job done. I am just exited about the broadcast . syntax, and wonder if it can be extend.
I think you're looking for mapslices. mapslices(x->x*x, a, (1,2)) 2×2×2 Array{Int64,3}: [:, :, 1] = 7 15 10 22 [:, :, 2] = 67 91 78 106 mapslices(f, A, dims) Transform the given dimensions of array A using function f. f is called on each slice of A of the form A[...,:,...,:,...]. dims is an integer vector specifying where the colons go in this expression. The results are concatenated along the remaining dimensions. For example, if dims is [1,2] and A is 4-dimensional, f is called on A[:,:,i,j] for all i and j. Use setdiff if you want to specify which dimension to concatenate along instead of on which to apply the function. (If you need a multi-argument version check out this gist https://gist.github.com/alexmorley/e585df0d8d857d7c9e4a5af75df43d00)
How to print output of .bas file to text
I am trying to print coordinate outputs of a program to a text file in order to use it in another program but I don't really know anything about GWBASIC and its my first time using MS-DOS. I need it to open a text file named plot.txt and print output there and save it without actually plotting on GWBASIC. Here is the program which I found in an old magazine. 810 REM MAKE A GLOBULAR 12 REM 14 R0=20: R2=R0*R0: R3=R2*R0 16 P1=3.14159265# 18 C0=P1*P1*R3/4 20 R1=R0/SQR(2) 22 XM=512: YM=512 24 X2=XM/2: Y2=YM/2: S=5 26 INPUT "HOW MANY STARS ";T 27 RANDOMIZE TIMER 28 CLS: REM CLEAR SCREEN 30 FOR I=1 TO T 32 C=C0*RND: R=R1 34 REM 36 REM NOW FIND R 38 FOR K=1 TO 5 40 GOSUB 100 42 R=R+(C-C1)/D 44 NEXT K 46 REM 3-DIMENSIONAL PLACE 48 X=RND-.5 50 Y=RND-.5 52 Z=RND-.5 54 S1=SQR(X*X+Y*Y+Z*Z) 56 IF S1>.5 THEN GOTO 48 58 REM POINT IS NOW IN SPHERE 60 R=R*S1: X=X*R: Y=Y*R: Z=Z*R 62 GOSUB 200 64 NEXT I 66 END 68 REM 100 REM NEWTON-RAPHSON ITERATION 105 A=R/R0 110 C1=ATN(A)*.5*R3 115 A=1+A*A 120 C1=C1+R*.5*R2/A 125 C1=P1*(C1-R*R2/(A*A)) 130 D=4*P1*R*R/(A*A*A) 135 RETURN 140 REM 200 REM 2-DIMENSIONAL PLOT 203 SCREEN 9 205 X=X*S+X2: Y=Y*S+Y2 210 IF X<0 OR Y<0 THEN 225 215 IF X>=XM OR Y>=YM THEN 225 220 PSET(X,Y) 225 RETURN 230 REM ------------------------ 240 REM APPEARED IN ASTRONOMICAL 250 REM COMPUTING, SKY & TELE- 260 REM SCOPE, APRIL, 1986 270 REM ------------------------
Here is a Python 3 paraphrase: #globular.py #Python paraphrase of model.bas from #http://www.skyandtelescope.com/wp-content/uploads/model.bas from math import pi, sqrt, atan from random import uniform, random #Global variables: r0 = 20.0 r2 = r0**2 r3 = r0**3 c0 = pi**2*r3/4 r1 = r0/sqrt(2) def NRI(c,r): #Newton-Raphson Iteration a = r/r0 c1 = atan(a)*0.5*r3 a = 1+a**2 c1 += r*0.5*r2/a c1 = pi*(c1-r*r2/a**2) d = 4*pi*r**2/a**3 return (c1,d) def makeStars(t): stars = [] for i in range(t): c = c0*random() r = r1 for k in range(5): c1,d = NRI(c,r) r += (c-c1)/d while True: x = uniform(-0.5,0.5) y = uniform(-0.5,0.5) z = uniform(-0.5,0.5) s1 = sqrt(x**2 + y**2 + z**2) if s1 <= 0.5: break r *= s1 x *= r y *= r z *= r stars.append((x,y,z)) return stars def starsToFile(t,fname): stars = makeStars(t) f = open(fname,'w') for star in stars: print(*star, sep = ', ',file = f) f.close() I skipped the part about printing x and y and instead wrote a function makeStars to return a list of (x,y,z) tuples, as well as a related function which takes such an output and sends it to a text file. This last function is the only thing that used Python 3 instead of Python 2. If you are using Python 2 you can import Python 3's print function from the future. Typing starsToFile(100,'stars.txt') in the Python shell gave me a text file which begins: -0.32838465248713156, -0.3294895266926551, -1.2963580524762535 14.20224408569865, 1.4434961933043464, 6.450969593697097 1.6525937589658193, -0.24447292610082685, 1.0543647986350608 1.5707528567123823, 5.190972598268825, -2.0054790217091134 I don't have good 3-d scatter-plot graphing at my finger tips, but here is a screen shot of 50 points generated by the function and plotted using a computer algebra system called Derive: Final remark: I wonder if there is a typo in the source code. The line C0=P1*P1*R3/4 strikes me as suspicious since it is fairly rare in mathematics for pi to appear squared -- though it does happen. Maybe there should be only 1 factor of pi there (which would then have the effect of setting C0 proportional to the volume of the sphere of radius R0). On the other hand, I don't know exactly what is happening here, so I left it in. If the results seem problematic, you could maybe experiment with that line.
If you want a copy of the calculated coordinates simply add these lines: 1 OPEN "PLOT.TXT" FOR OUTPUT AS #1 65 CLOSE #1 221 PRINT #1, X + "," + Y The program will work as before but in addition to this it outputs the coordinate to a file named plot.txt Put them in an image with 640x350 size (that size is demanded by SCREEN 9) and you get the same result.
Are these test cases correct?
While working on the google foobar problems, I ran into one with the following prompt: Professor Boolean, a notorious mad scientist, just found out his precious rabbit specimen has escaped! He rushes to call his security minions on the lab phone. However, the rabbit escapee hacked the phone to speed her escape! She left a sign with the following clues: Each digit that is dialed has to be a number that can be reached by a knight chess piece from the last digit dialed - that is, you must move precisely 2 spaces in one direction, and then 1 space in a perpendicular direction to dial the next correct number. You can dial any number you want to start with, but subsequent numbers must obey the knight's move rule. The lab phone has the numbers arranged in the following way: 1, 2, 3 in the first row; 4, 5, 6 in the second row; 7, 8, 9 in the third row; and blank, 0, blank in the fourth row. 1 2 3 4 5 6 7 8 9 0 For example, if you just dialed a 1, the next number you dial has to be either a 6 or an 8. If you just dialed a 6, the next number must be a 1 or 7. Professor Boolean wants you to find out how many different phone numbers he can dial under these conditions. Write a function called answer(x, y, z) that computes the number of phone numbers one can dial that start with the digit x, end in the digit y, and consist of z digits in total. Output this number as a string representing the number in base-10. x and y will be digits from 0 to 9. z will be between 1 and 100, inclusive. Test cases Inputs: (int) x = 6 (int) y = 2 (int) z = 5 Output: (string) "6" Inputs: (int) x = 1 (int) y = 5 (int) z = 100 Output: (string) "0" Inputs: (int) x = 3 (int) y = 7 (int) z = 1 Output: (string) "0" I feel that the first test case is wrong because after drawing out the decision tree the only numbers I find are possible are 61672, 67272, 67292, 67672, which is only four. Is there something I'm misinterpreting or missing about this problem?
I don't think the test case is wrong, but the statement If you just dialed a 6, the next number must be a 1 or 7. is wrong. Not being able to get from 6 to 0 makes the problem not make any sense. As to your comment, blanks aren't valid moves, but what does that have to do with 6 -> 0 being valid? 1 2 3 4 5 6 7 8 9 0 Obviously 6 -> 0 is a valid move, no blanks involved.
Moving between hexadecimal and binary notation
When one byte is represented by 8 bits in binary notation you have a sequence of 8 possible 1's and 0's. So 00101010 can be shortened to 2A using hexadecimal notation. My book says you can shorten that representation by using hexadecimal after the 4th place from the right. For example... 00101010 can be represented with a mix of hexadecimal notation and binary notation by taking the 4 digits on the left 0010 and and representing that sequence to equal 2 in hexadecimal. I understand because 0010 equals 32 and when you are using hexadecimal notation that has a base of 16 that equals to 2. What I don't understand is how the right side of the sequence is represented. My book says 1010 can be represented by the letter A which equals to 10. 1010 in binary notation equals 8 + 2 = 10. Here is the issue I'm having. Applying the same concept to the right side as the left side of the 8 bit sequence shouldn't you divide the ride side 10 by 2 since binary notation is using the power of 2 like you divided the left side by 16 since you're using hexadecimal notation which has the power of 16? Am i thinking about it wrong?
Let's start with the complete 8-bit byte, writing the place value under each digit: 0 0 1 0 1 0 1 0 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 8 4 2 1 ↓ ↓ ↓ 16 ↓ ↓ 32 ↓ 64 128 So, in base 10, this is 32 + 8 + 2 = 42. If we split the 8-bit byte into two 4-bit nybbles, you have 0 0 1 0 1 0 1 0 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 8 4 2 1 8 4 2 1 You'll notice each 4-bit nybble can hold a value from 0 to 15. So the nybbles can represent two hexadecimal digits. We treat the two nybbles the same way when we calculate their values. From left to right, the digits in each nybble have place values 8, 4, 2, 1. So the upper (left) nybble has a value of 2, and the lower (right) nybble has a value of 8 + 2 = 10. As you wrote, the decimal number 10 is written as A in hexadecimal, so the hexadecimal byte is written 2A. Remember, though, that this is hexadecimal. So the places values are powers of 16: 2 A ↓ ↓ ↓ 1 16 So, converting back to decimal, 2A = 2×16 + 10 = 32 + 10 = 42.
When converting binary to hexadecimal, the left 4 bits gets the same treatment as the right 4 bits. All that "binary to hex" does is replacing any sequence of 4 bits with a single hexadecimal digit -- at that stage you don't have to worry about conversion to a 'full' number. Your example 00101010 can be split in two 4-bit sequences: 0010 and 1010. Converting each of these into hex is done by adding them up from right to left, multiplying each next bit by two. This is exactly what you ate used to in base-10; a value 532 represents "10^0 * 2 + 10^1 * 3 + 10^2 * 5" (where ^ is the usual shorthand for "power of"). So, for the first 4 bits you get 0*1 + 1*2 + 0*4 + 0*8 = 2 and the second set of 4 bits 0*1 + 1*2 + 0*4 + 1*8 = 10 In hexadecimal, each value from 0 to 15 is represented by a single 'digit', and we run out of single digits at 9. So starting at 10, we use A,B,C,D,E, and F to represent the decimal values 10, 11, 12, 13, 14, and 15. Hence, the hex representation of 1010 is A; and your binary number translates to 2A. Converting this, in turn, to decimal again also works the same as in decimal, only now each 'digit' represents a next power of 16. So this evaluates as 16 * 2 + 1 * A or (decimal) 16 * 2 + 1 * 10 = 42 You can verify that this is the same decimal value as the starting binary 00101010 by adding up all of its binary numbers: 1 * 0 2 * 1 4 * 0 8 * 1 16 * 0 32 * 1 64 * 0 128 * 0