If I have a 2-D array A and idx is an index vector or value, and I want to assert that
A[idx,:]
be a 2D array (even if idx is a number), is there an easy way to do this without always having to divide into cases where idx is a list or idx is a number.
I have found a workaround which is to define a ‘safe’ script
Is there a more elegant way? [e.g., explicitly asserting type of idx in a careful way such as:
idx :: Union{UnitRange{Int64},Array{Int64,1}}
]
I am of the opinion that usage of this should be easier than it is in Julia, but accept that I have obviously lost on that point. However, surely explicit assertion on a case-by-case basis should be possible (and allowed)?
Is there an easy way?
Thanks - If there were one single thing in Julia which I could honestly say has repeatedly caused me frustration in Julia (and which continues to prevent me from using Julia in my elementary classes) this would be it.
Thanks for that. The answer to your question is ‘yes’.
I guess what I would really hope for would be a new Type (perhaps only available locally) of Array which would automatically behave like this.
Almost as good would be a Vector Type which would never reduce to Int64
(as this could be used as an index).
(What is your reason for using findall on BitArray indices, btw?)
But, when do you get into problems like this? The type of the input index should normally be under control, so if indices are scalar, it should be easy for you to know this, and use
A[idx:idx, :]
Also, what do you want A[:, idx] to return, btw, for scalar idx? Do you want a vector or matrix?
I missed this. This is what we have today. Vectors don’t reduce to scalars, so if you have a vector of length 1, it is still a vector and can be used to index the way you want.
Thanks, that is simpler. If idx might be the result of a search, etc. then it could be vector or integer, in a way which might not be predicted beforehand.
If one has many lines like this in a script, making special cases for each is very clunky. At least the ‘safe index’ (s()) workaround, which you helpfully simplified, is quite a bit less clunky (and more readable as code).
Thanks for that. I will have a look at my code. I’ll admit I had written my own wrapper for findall (with a calling syntax I find more convenient), but perhaps I
hadn’t seen this discrepancy between it and findall (which in any case, is easily fixable).
Thus far, I have used a combination of MATLAB, R, and Python, so switching to Julia would be handy. They come from a background which includes these 3, so I have been writing some ‘compatibility’ routines which make it easier for me. Maybe it is possible for me to make them work more consistently after all.
I just implemented the changes which should cover the issues in most of the cases, and for those few issues which may not be covered, I now have a very simple ‘s()’ function above which will come in very handy!