I would like to build a function, that iterates through values of a Julia "zip" file and substitutes some values in a large 3d array. In a previous help someone wise suggested the use of @view
and I quite like the idea of writing back to the original section of the array rather than making a copy. If this is good method (happy to take advice), I can't figure out the correct syntax for the function, that exploits @view
. This is a demo:
Original code, which works well, will be many more in final application
(@view Pop[end, :, 1])[findall(x -> x==33, Pop[end, :, 1])] .= 3
(@view Pop[end, :, 1])[findall(x -> x==44, Pop[end, :, 1])] .= 4
It simply substitutes 33 -> 3, and 44 -> 4 in the array Pop[end, :, 1]
So I produce the zip file:
Orig = [44, 33];
NewS = [4 , 3];
ResetZip = zip(Orig,NewS)
Then produce the function:
## Function to reset status numbers back from 44 and 33 to 4 and 3
function ResetState(Arr1, Orig, NewS)
Arr1[findall(x -> x==Orig, Arr1)] .= NewS
end
Then try to execute the function iteratively over values of ResetZip
for (Orig, NewS) = ResetZip
ResetState(@view (Pop[end, :, 1]), Orig, NewS)
end
but I get this error
ERROR: LoadError: ArgumentError: Invalid use of @view macro: argument must be a reference expression A[...].
So I've got something wrong in the syntax. My question is, where do I put the @view
to get the syntax correct, to be able to use it, and iterate over values of the zip file? Thx. J
question from:
https://stackoverflow.com/questions/65889525/julia-syntax-to-use-views-in-a-function