I was wondering if there is an option to simply generate a range of numbers from 1 to N in Netezza?
Let's say that N=5, then my result should be:
N
1
2
3
4
5
Thanks!
select _v_vector_idx.idx where idx between 1 and 10;
Note: IDX range is 0-1023
Related
I was looking for a way to circshift using index values.
I know I can shift all values using the circshift command see below
a=[1:9]
b=circshift(a,[0,1])
But how do I shift every 3rd value over 1?
example:
Note: variable a can be any length
a=[1,2,30,4,5,60,7,8,90] %note variable `a` could be any length
I'm trying to get b to be
b=[1,30,2,4,60,5,7,90,8] % so the values in the index 3,6 and 9 are shifted over 1.
You're not going to be able to do this with the standard usage of circshift. There are a couple of other ways that you could approach this. Here are just a few.
Using mod to create index values
You could use mod to subtract 1 from the index values at locations 3:3:end and add 1 to the index values at locations 2:3:end.
b = a((1:numel(a)) + mod(1:numel(a), 3) - 1);
Explanation
Calling mod 3 on 1:numel(a) yields the following sequence
mod(1:numel(a), 3)
% 1 2 0 1 2 0 1 2 0
If we subtract 1 from this sequence we get the "shift" for a given index
mod(1:numel(a), 3) - 1
% 0 1 -1 0 1 -1 0 1 -1
Then we can add this shift to the original index
(1:numel(a)) + mod(1:numel(a), 3) - 1
% 1 3 2 4 6 5 7 9 8
And then assign the values in a to these positions in b.
b = a((1:numel(a)) + mod(1:numel(a), 3) - 1);
% 1 30 2 4 60 5 7 90 8
Using reshape.
Another option is to reshape your data into a 3 x N array and flip the 2nd and 3rd rows, then reshape back to the original size. This option will only work if numel(a) is divisible by 3.
tmp = reshape(a, 3, []);
% Grab the 2nd and 3rd rows in reverse order to flip them
b = reshape(tmp([1 3 2],:), size(a));
I'm trying to create a script to solve my problem, but I got stuck in one place.
So I have imported .txt file with 4x1 sized matrix (simplified to give an example in my case it might be 1209x1 matrix) which contains some coordinate X. And it's look like this:
0
1
2
3
That's coordinates for one period, and I need to get one column for different number of periods N . Each period is the same and lenght=L
So you can do it manually by this script, for example for N=3 periods:
X=[X; X+L; X+2*L];
so for example if L=3
then i will get
0
1
2
3
3
4
5
6
6
7
8
9
it works well but it's not efficient in case if I need to work with number of periods let's say N=1000 or if I need to change their number quickly. Any solution to parameterize this operation so I can just put number for N and get X for N periods?
Thanks and Regards
I don't have MATLAB on this machine so I can't test, but the most straightforward implementation would be something like:
n = 1000;
L = 3;
nvalues = length(X); % Assuming X is your initial vector
newx = zeros(n*nvalues, 1); % Preallocate new array
for ii = 0:(n-1)
startidx = (nvalues*ii) + 1;
endidx = nvalues*(ii+1);
newx(startidx:endidx) = X + ii*L
end
You can use bsxfun to create X, X+L, X+2*L, ... and then reshape it to a vector
>> F=bsxfun(#plus, X, (0:(N-1))*L)
F =
0 3 6
1 4 7
2 5 8
3 6 9
>> X=F(:)
X =
0
1
2
3
3
4
5
6
6
7
8
9
or in a more concise form:
>> X=reshape(bsxfun(#plus, X, (0:(N-1))*L), [], 1)
X =
0
1
2
3
3
4
5
6
6
7
8
9
Given a vector
X = [1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3]
I would like to generate a vector such
Y = [1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5]
So far what I have got is
idx = find(diff(X))
Y = [1:idx(1) 1:idx(2)-idx(1) 1:length(X)-idx(2)]
But I was wondering if there is a more elegant(robust) solution?
One approach with diff, find & cumsum for a generic case -
%// Initialize array of 1s with the same size as input array and an
%// intention of using cumsum on it after placing "appropriate" values
%// at "strategic" places for getting the final output.
out = ones(size(X))
%// Find starting indices of each "group", except the first group, and
%// by group here we mean run of identical numbers.
idx = find(diff(X))+1
%// Place differentiated and subtracted values of indices at starting locations
out(idx) = 1-diff([1 idx])
%// Perform cumulative summation for the final output
Y = cumsum(out)
Sample run -
X =
1 1 1 1 2 2 3 3 3 3 3 4 4 5
Y =
1 2 3 4 1 2 1 2 3 4 5 1 2 1
Just for fun, but customary bsxfun based alternative solution -
%// Logical mask with each column of ones for presence of each group elements
mask = bsxfun(#eq,X(:),unique(X(:).')) %//'
%// Cumulative summation along columns and use masked values for final output
vals = cumsum(mask,1)
Y = vals(mask)
Here's another approach:
Y = sum(triu(bsxfun(#eq, X, X.')), 1);
This works as follows:
Compare each element with all others (bsxfun(...)).
Keep only comparisons with current or previous elements (triu(...)).
Count, for each element, how many comparisons are true (sum(..., 1)); that is, how many elements, up to and including the current one, are equal to the current one.
Another method is using the function unique
like this:
[unqX ind Xout] = unique(X)
Y = [ind(1):ind(2) 1:ind(3)-ind(2) 1:length(X)-ind(3)]
Whether this is more elegant is up to you.
A more robust method will be:
[unqX ind Xout] = unique(X)
for ii = 1:length(unqX)-1
Y(ind(ii):ind(ii+1)-1) = 1:(ind(ii+1)-ind(ii));
end
I am having trouble averaging y values based on their x counter parts.
For example
1 5
3 4
1 6
How do I get 5 and 6 to average based on being paired with an x value of 1? For my specific issue I will have 98 values between repeating 1's, and there will be a total of 99 1's in the array.
This is not extremely complicated, but it has been over a year since I have used matlab so being rusty has me scratching my head.
Here's what I got:
x = [1, 5;
3, 4;
1, 6]
col1 = x(:, 1) % extract first row
col1 =
1
3
1
ri = find(col1 == 1) % get row indices where 1 appears
ri =
1
3
mean(x(ri, 2)) % index into the second column of rows with a 1, and take average
ans = 5.5000
I generate a vector of 20 random integers in 1 : 6. For more clarity: d = floor ( 6 * rand ( 1 , 20 ) + 1). How can I count the numbers of sixes with MATLAB?
Just use this -
count = nnz(d==6)
One of the uses of nnz is to count the number of matches found. In this case, it would do comparisons between every element of d with 6 and return a logical array of ones or zeros based on the matches being found or not respectively and then nnz would count the number of occurrences of ones. nnz is really a very efficient tool for such cases, try exploring it.
nnz(d==6)
As given by Divakar is great. But using sum is usually faster:
sum(d(:)==6)
Example:
d = floor ( 6 * rand ( 1 , 2e6 ) + 1);
tic;nnz(d==6);toc;
tic;sum(d(:)==6);toc;
gives:
Elapsed time is 0.020109 seconds.
Elapsed time is 0.012709 seconds.
If you just want to count the number of occurrences of a single value then use nnz(d==6) as #Divakar suggested, but if you want to count the total number of multiple values, 3s and 6s say, you can do that with ismember:
num3s6s = nnz(ismember(d,[3 6]))
same as:
num3s6s = nnz(d==3 | d==6)
If you want to obtain the count of each value: use histc:
d = [1 2 4 2 3 4 5 4 3 6]; %// example data
values = 1:6; %// values you want the count of
count = histc(d, values);
This gives
count =
1 2 2 3 1 1