I’m trying to write a simple function that replaces the corners of an array with a value. This is what I have so far:
function replaceCorners!(a::AbstractArray{String, 2}, v::String)
a[1, 1] = a[1, size(a)[2]] = a[size(a)[1], 1] = a[size(a)...] = v
end
I have a few questions about this problem, especially because I’m new to Julia.
Is this the most “julian” way to replace the corners of an array?
This method works for an array that looks like ["a" "b" "c"], but not for an array such as ["a"; "b"; "c"]. How can I make it work for arrays of any size?
In the function above, I only allowed for arrays that contain strings and values that are strings - how would I go about making this applicable to any type?
Thanks for the alternative solution! I’m having some trouble figuring out what exactly is happening on the third line.
With an example array like this
a = [
12 11 10 9;
8 7 6 5;
4 3 2 1;
]
Running firstlast.(axes(a)) gives me (1:2:3, 1:3:4). This makes sense, but splatting that then indexing a by the result is hard to visualize. Secondly, what is the significance of using (v,)?
I actually had the following at first (before a ninja edit):
firstlast(x) = [first(x), last(x)]
That is, I just want an array with two elements. The above isn’t ideal because it’ll actually allocate an Array for every dimension. I swapped to a cleverly constructed StepRange that contains the same elements — but will probably avoid allocations entirely.
So I splat that into the array, which effectively does:
a[[1,end],[1,end],[1,end], #=etc=#]
This selects the cartesian product of all the endpoints of each dimension in the array. For a two-dimensional array, there are 4 (2*2) corners. A cube has 8 (2*2*2). And so on.
Then I broadcast the value v into all those locations I’ve selected. If v itself is broadcastable container, well, then it’ll try to match its shape with the shape of the indices I used — potentially “spreading” its values out amongst the selected locations (but more likely just throwing a shape mismatch error). Using (v,) “protects” the value inside a container I know is only one element and ensures that v itself is assigned to each and every single chosen location.
Also note that I’m not restricting the type of v at all. Upon assignment, Julia will attempt to convert v to the element type of the array for you — and it’ll happily throw an error at that point if it’s not possible.