I'm completely new to coding and have almost no idea what I'm doing. Currently I'm trying to find the radius of each pixel from the center of a galaxy in a fits file. I was told to try this by creating a blank array the same size as the fits file and I'm trying to use a for loop for each x value and another for each y. So far, I have the array and have attempted to create the for loops.
xcenter =249.8
ycenter =250.0
d=fltarr(500,500)
for i=0,499 do begin
d=d(i-xcenter,*)
endfor
for j=0,499 do begin
d=d(j-ycenter,*)
endfor
I know this look awful and I obviously have no idea what I'm doing. So if anyone can offer any help at all I'd be grateful.
Let's look at a simpler version first. Suppose you have 10 points on a line, and you want to calculate the distance of each point from some xref. IDL supports vector math, so you can do this:
xref = 5.4
x = dindgen(10)
distance = abs(x - xref)
I have used the DINDGEN command here, and you can look up the help. Now in a 2d case, you want 2-d arrays: one will be a 500 * 500 array containing the X coordinate of each pixel, the other containing the Y coordinate. So the arrays will have to be of the form,
0 1 2 3 ...
0 1 2 3 ...
0 1 2 3 ...
and
0 0 0 0 ...
1 1 1 1 ...
2 2 2 2 ...
We can generate them using the # operator. Note that IDL counts from 0 by default.
just_one = replicate(1d, 500) ; contains 1 1 1 1 ...
one_500 = dindgen(500) ; contains 0.0 1.0 2.0 ...
x = just_one # one_500
y = one_500 # just_one
Now you can calculate the distance as usual, d = sqrt(xx + yy), but using vector math again:
distance = sqrt( (x - xref) ^ 2 + (y - yref) ^ 2 )
This is a 500x500 array, which contains the distance of each pixel from your xref, yref points.
Related
I hope you can help me with my request.
I have an integer input from a client. Lets say 250. The goal is to calculate the input to exactly 0 by only using 3 variables. x = 30, y = 50 and z = 100 and output how often i used each of them.
The given rules are:
I have to use as less of them as possible. means, first the z's then the y's then the x's. So i have to use as much as possible of the variables with greater values.
Im allowed to use a maximum of 3 of each of them.
I have to count how often i used each of them.
If a combination is not possible, lets say 70 ( 70 - y = 20, 70 - x - x = 10 ), give a message to the client, "Invalid input!".
Few examples:
Input = 250
Output:
z = 2
y = 1
####################
Input = 170
Output: Invalid input!
Because i can only calculate to zero by using 4 x's and 1 y. See rule 2.
Other solutions are not possible.
i.e. Input - z - y = 20 => error
Input - y - y - y = also 20 => error
####################
Input = 90
Output:
x = 3
####################
Input = 120
Output: Invalid input!
Input - z = 20 => error
Input - 50 - 50 = 20 => error
Input - x - x - x - x => See rule 2.
####################
and so on....
And how it could be extended if a further value is added to the 3 variables. i.e. 40
So if thats the case it should be somehowe dynamically.
I tried to explain that as much as possible. If you miss infos, just let me know.
Thank you.
dagogi
I have a series of values that are each being stored as UInt16. Each of these numbers represents a bitmask - these numbers are commands that have been sent to a microprocessor telling it which pins to set high or low. I would like to parse this arrow of commands to find out which pins were being set high each time in such a way that is easier to analyse later.
Consider the example value 0x3c00, which in decimal is 15360 and in binary is 0011110000000000. Currently I have the following function
function read_message(hex_rep)
return findall.(x -> x .== '1',bitstring(hex_rep))
end
Which gets called on every element of the array of UInt16. Is there a better/more efficient way of doing this?
The best approach probably depends on how you want to handle vectors of hex-values. But here's an approach for processing a single hex which is much faster than the one in the OP:
function readmsg(x::UInt16)
N = count_ones(x)
inds = Vector{Int}(undef, N)
if N == 0
return inds
end
k = trailing_zeros(x)
x >>= k + 1
i = N - 1
inds[N] = n = 16 - k
while i >= 1
(x, r) = divrem(x, 0x2)
n -= 1
if r == 1
inds[i] = n
i -= 1
end
end
return inds
end
I can suggest padding your vector into a Vector{UInt64} and use that to manually construct a BitVector. The following should mostly work (even for input element types other than UInt16), but I haven't taken into account specific endianness you might want to respect:
julia> function read_messages(msgs)
bytes = reinterpret(UInt8, msgs)
N = length(bytes)
nchunks, remaining = divrem(N, sizeof(UInt64))
padded_bytes = zeros(UInt8, sizeof(UInt64) * cld(N, sizeof(UInt64)))
copyto!(padded_bytes, bytes)
b = BitVector(undef, N * 8)
b.chunks = reinterpret(UInt64, padded_bytes)
return b
end
read_messages (generic function with 1 method)
julia> msgs
2-element Vector{UInt16}:
0x3c00
0x8000
julia> read_messages(msgs)
32-element BitVector:
0
0
0
0
0
0
0
0
0
⋮
0
0
0
0
0
0
0
1
julia> read_messages(msgs) |> findall
5-element Vector{Int64}:
11
12
13
14
32
julia> bitstring.(msgs)
2-element Vector{String}:
"0011110000000000"
"1000000000000000"
(Getting rid of the unnecessary allocation of the undef bit vector would require some black magic, I belive.)
My function and rounding to nearest even number
function y = rndeven(x)
if x<=1
y=2;
else
y = 2*floor(x);
end
endfunction
When I run it I get:
cc=[0:3]'
both=[cc,rndeven(cc)]
0 0
1 2
2 4
3 6
What I'm trying to get as the Result:
0 2
1 2
2 2
3 4
You can use the modulo 2 to find whether a number is even. If it isn't this will return 1, so just add 1 to this number to find the nearest (larger) even number:
function y = rndeven(x)
x = floor(x);
x(x <= 1) = 2;
y = mod(x,2)+x;
end
This works for any array, order of elements does not matter.
You could also check if it is dividable by 2 if you don't want to use the mod function. The pseudo code would be something like this:
while(x % 2 != 0) x = x + 1
return x
I am new to programming and I would like to know how to solve questions like this. I was told to expect questions like this on the exam. Can someone please tell me how I would go about solving something like this? Thanks.
x = 0
for num in range(5):
if num % 2 == 0:
x = x + 2
else:
x = x + 1
print(x)
You need to work on a skill which is to "be the compiler", in the sense that you should be able to run code in your head. Step through line by line and make sure you know what is happening. In you code example, you have
for num in range(5) means you will be iterating with num being 0,1,2,3 and 4. Inside the for loop, the if statement num % 2 == 0 is true when num/2 does not have a remainder (how % mods work). So if the number is divisible by 2, x = x+2 will execute. The only numbers divisible by 2 from the for loop are 0,2 and 4. so x=x+2 will execute twice. The else statement x = x +1 runs for all other numbers (1,3) which will execute 2 times.
Stepping through the for loop:
num = 0 //x=x+2, x is now 2
num = 1 //x=x+1, x is now 3, print(x) prints 3
num = 2 //x=x+2, x is now 5
num = 3 //x=x+1, x is now 6, print(x) prints 6
num = 4 //x+x+2, x is now 8
Therefore the answer is that 3 and 6 will be printed
In my opinion,
Whatever language you are using, you need to learn some common elements of the modern programming languages, such as flow-control (if...else in your case), loop(for, in your case)
Some common used functions, in your case, you need to what does range do in Python,
docs.python.org is a good place for you.
As you are new to programming, you can go with the flow in you mind or draw it on the paper.
Using x to store our final result
loop through every item in [0, 1, 2, 3, 4] <- range(5)
a. if
the number is divisible by 2
then increase x by adding 2 to it.
b. else
increase x by adding 1 and print it out
So the result would be :
3
6
I am trying to understand if it's possible to use Octave more efficiently by removing the for loop I'm using to calculate a formula on each row of a matrix X:
myscalar = 0
for i = 1:size(X, 1),
myscalar += X(i, :) * y(i) % y is a vector of dimension size(X, 1)
...
The formula is more complicate than adding to a scalar. The question here is really how to iterate through X rows without an index, so that I can eliminate the for loop.
Yes, you can use broadcasting for this (you will need 3.6.0 or later). If you know python, this is the same (an explanation from python). Simply multiply the matrix by the column. Finnaly, cumsum does the addition but we only want the last row.
newx = X .* y;
myscalars = cumsum (newx, 1) (end,:);
or in one line without temp variables
myscalars = cumsum (X .* y, 1) (end,:);
If the sizes are right, broadcasting is automatically performed. For example:
octave> a = [ 1 2 3
1 2 3
1 2 3];
octave> b = [ 1 0 2];
octave> a .* b'
warning: product: automatic broadcasting operation applied
ans =
1 0 6
1 0 6
1 0 6
octave> a .* b
warning: product: automatic broadcasting operation applied
ans =
1 2 3
0 0 0
2 4 6
The reason for the warning is that it's a new feature that may confuse users and is not existent in Matlab. You can turn it off permanentely by adding warning ("off", "Octave:broadcast") to your .octaverc file
For anyone using an older version of Octave, the same can be accomplished by calling bsxfun directly.
myscalars = cumsum (bsxfun (#times, X, y), 1) (end,:);