Problems defining an array in SAS - arrays

I'm trying to define an array using the following code:
data all_dates;
array arr[*] _temporary_ (1 2 3 4 5);
run;
It gives me this message:
ERROR: The non-variable based array arr has been defined with zero elements.
Looking at the documentation examples I can't see why this wouldn't work. Am I doing something wrong or is this just not allowed? If it's not allowed what is an alternative equivalent method?

Ah nevermind - the documentation actually does state:
You cannot use the asterisk with _TEMPORARY_ arrays or when you define a multidimensional array.
I guess I'll have to count the number of elements I'll need beforehand and then use:
data all_dates;
array arr[*] a1-a5 (1 2 3 4 5);
drop a1-a5;
run;

The general solution with temporary arrays is to over-define the temporary array. Since it's not in the PDV, and not written out (obviously), you can simply define it for 50 or 100 or whatever is a safe number above your maximum - it won't cost almost any performance to do so.
Alternately, if the 'safe number' is too large for your desired memory allocation (say, tens of thousands), use a macro variable and precount the number. Given that you wrote out
(1 2 3 4 5)
You should be able to count the number of needed variables when you create this list.

Related

Check all the values in a Julia array?

How can I check all the values in a Julia array at once? Let's say I have an array like a=[3,4,6,10,55,31,9,10] How can I check if the array has any values greater than 10? Or how can I check if there are repeating values (like the 10 that is contained twice in the sample? I know I can write loops to check this, but I assume Julia has a faster way to check all the values at once.
The functions any and count do this:
julia> a = [3,4,6,10,55,31,9,10]
8-element Array{Int64,1}:
3
4
6
10
55
31
9
10
julia> any(x->x==3, a)
true
julia> count(x->x==10, a)
2
However the performance will probably be about the same as a loop, since loops in julia are fast (and these functions are themselves implemented in julia in the standard library).
If the problem has more structure you can get big speedups. For example if the vector is sorted you can use searchsorted to find matching values with binary search.
Not sure if this had been implemented at the time of previous answers, but the most concise way now would be:
all(a .> 10)
As Chris Rackauckas mentioned, a .> 10 returns an array of booleans, and then all simply checks that all values are true. Equivalent of Python's any and all.
You can also use broadcasted operations. In some cases it's nicer syntax than any and count, in other cases it can be less obvious what it's doing:
boola = a.>10 # Returns an Array{Bool}, true at any value >10
minimum(boola) # Returns false if any are <10
sum(a-10 .== 0) # Finds all values equal to 10, sums to get a count

matlab rearrange (permute) string array

I have a string array:
size(entries)
ans =
1 19413
I would like to rearrange the array to 4853 rows and 4 columns:
output=permute(entries,[4853 4]);
but get following error:
Error using permute ORDER contains an invalid permutation index.
What is the (probably obvious thing) I am doing wrong? thanks
You currently have 19413 elements, yet you wish to reshape this into a 4853 x 4 matrix that consists of 4853 * 4 = 19412 elements. No function in the world will help you do this because the original and target amount of elements don't match - they're off by one element. If you remove one of the elements...say... the last one, then we're getting somewhere.
Supposing you made a mistake and included that extra element by accident, you don't use permute here, but you use reshape. The second argument to reshape is the amount of elements to spread out for each target dimension, and that's what you're looking for. First remove the extraneous element that appears at the end of the array, then reshape the matrix:
output = reshape(entries(1:end-1),[4853 4]);
I'm 3 years late, but here's to anyone still looking for an answer.
In your case as mentioned above, yes you should use reshape() while minding that you preserve the total number of elements.
You use permute() when you want to reorder the dimensionality of an n-dimensional (ND) matrix.
The ORDER parameter specifies the order of the columns.
For example, if matrix A is LxMxN, the following line would make it MxLxN.
A = permute(A,[2 1 3]);
Hope this clears things.

How to slice array in GUI function?

Because I am trying to let a GUI element slice my array, there will be a : (colon) sign in the variables. This returns me an error:
Error in gui_mainfcn (line 96)
feval(varargin{:});
line 96 refers to this code:
image(handles.data(1:handles.rows,1:handles.cols, temp))
Temp looks like this
temp =
1 1 1 1 2 1 1 1 1
And both handles.rows and cols are the value 64. So the problem seems to be that I use colons in the gui function. However, to slice I need to use colons. My question now is: Any idea how to work around this?
To clarify as requested below
The above code works when I manually enter it in the console. Also when I use handles.data(:,:,1,1,1,1,2,1,1,1,1), handles.data(1:end,1:end,1,1,1,1,2,1,1,1,1), handles.data(1:64,1:64,1,1,1,1,2,1,1,1,1), etc I get the same error from the gui. Manually they all work and return a 64 by 64 array of doubles which I can plot with image().
Might be related to these questions, however those deal with parfor difficulties and dont seem to answer my question:
matlab-parfor-slicing-issue
index-inside-parfor-slicing
I am now also reading the advanced topics for slicing variables. Still dont see what I am doing wrong though, so any help or explanation would still be greatly apprectiated. Thanks!
Explanation
By putting the vector temp as the third index into your data, you are not indexing the higher dimensions - you are repeatedly indexing the third. In other words, you get handles.data(:,:,[1 1 1 1 2 1 1 1 1]) instead of handles.data(:,:,1,1,1,1,2,1,1,1,1).
Solution
Here's a solution that doesn't require squeeze or eval. It exploits the comma-separated lists output of the {:} syntax with cell arrays, and the ability to apply linear indexing on the last subscripted dimension.
ctemp = num2cell(temp); % put each index into a cell
sz = size(handles.data); % i.e. sz = [256 256 1 1 2 1 2]
sliceind = sub2ind(sz(3:end),ctemp{:}); % compute high dim. linear index (scalar)
image(handles.data(:,:,sliceind));
This performs subscripting of a >3D array with only 3 subscripts by computing the last subscript as a linear index. It's weird, but convenient sometimes.
A heads up for people with the same problem, this error can not only result from not knowing how to slice, it could also result from not having defined your variables correctly: http://www.mathworks.nl/matlabcentral/answers/87417-how-to-slice-inside-gui-without-error-feval-varargin

Reallocating/Erasing numpy array vs. new allocation in loop

In my program I need to work with arrays roughly 500x500 to 1500x1500 within a function that is looped over 1000's of times. In each iteration, I need to start with an array that has the same form (whose dimensions are fixed across all iterations). The initial values will be:
[0 0 0 ... 1]
[0 0 0 ... 1]
....
However, the contents of the array will be modified within the loop. What is the most efficient way to "reset" the array to this format so I can pass the same array to the function every time without having to allocate a new set of memory every time? (I know the range of rows that were modified)
I have tried:
a[first_row_modified:last_row_modified,:] = 0.
a[first_row_modified:last_row_modified,:-1] = 1.
but it takes roughly the same amount of time as just creating a new array every time with the following:
a = zeros((sizeArray, sizeArray))
a[:,-1] = 1.
Is there a faster way to effectively "erase" the array and change the last column to ones? I think this is similar to this question, clearing elements of numpy array , although my array doesn't change sizes and i didn't see the definitive answer to the previously asked question.
No; I think the way you are doing it is about as fast as it gets.

MATLAB: comparing all elements in three arrays

I have three 1-d arrays where elements are some values and I want to compare every element in one array to all elements in other two.
For example:
a=[2,4,6,8,12]
b=[1,3,5,9,10]
c=[3,5,8,11,15]
I want to know if there are same values in different arrays (in this case there are 3,5,8)
The answer given by AB is correct, but it is specific for the case when you have 3 arrays that you are comparing. There is another alternative that will easily scale to any number of arrays of arbitrary size. The only assumption is that each individual array contains unique (i.e. non-repeated) values:
>> allValues = sort([a(:); b(:); c(:)]); %# Collect all of the arrays
>> repeatedValues = allValues(diff(allValues) == 0) %# Find repeated values
repeatedValues =
3
5
8
If the arrays contains repeated values, you will need to call UNIQUE on each of them before using the above solution.
Leo is almost right, should be
unique([intersect(a,[b,c]), intersect(b,c)])
c(ismember(c,a)|ismember(c,b)),
ans =
3 5 8
I think this works for all matrices.
Define what you mean by compare. If the arrays are of the same length, and you are comparing equality then you can just do foo == bar -- it's vectorized. If you need to compare in the less than/greater than sense, you can do sign(foo-bar). If the arrays are not the same length and/or you aren't comparing element-wise -- please clarify what you'd like the output of the comparison to be. For instance,
foo = 1:3;
bar = [1,2,4];
baz = 1:2;
sign(repmat(foo',1,length([bar,baz])) - repmat([bar, baz],length(foo),1))
# or, more concisely:
bsxfun(#(x,y)sign(x-y),foo',[bar,baz])
does what you ask for, but there is probably a better way depending on what you want as an output.
EDIT (OP clarified question):
To find common elements in the 3 arrays, you can simply do:
>> [intersect(a,[b,c]), intersect(b,c)]
ans =
8 3 5

Resources