RxJava utility for ignoring items which are equal? - system.reactive

I'm trying to find the Observable operator in RxJava which will perform the following filter:
src: 0 0 5 5 5 5 0 0 0 1 0
dst: 0 5 0 1 0
I can build something, but I figured there must be something in the standard library that I'm overlooking.
distinct doesn't work because it only returns 0 5 1.

You're looking for distinctUntilChanged.

Related

How do I test a range in KDB?

Let's say I have a range 3 < x < 5.
What's the cleanest way to get 1 if x > 5, -1 if x < 3 and 0 if it's in the range 3 <x <5?
I'm trying to mix together & and | (trying to adapt the answer from here: In KDB/Q, how do I clip numbers to be in the range -1, 1?). I'm not sure if it's possible this way though.
You could adapt the bin keyword which does something like what you're looking for out of the box:
q)3 5 bin 0 1 2 3 4 5 6
-1 -1 -1 0 0 1 1
If you are looking for 0 to be returned for n where x < n < y then I think you may be able to do something like:
q)f:{[range;x] (range + 1 0) bin x}
q)range35:f[3 5;]
q)range35 0N!til 10
0 1 2 3 4 5 6 7 8 9
-1 -1 -1 -1 0 1 1 1 1 1
Which returns a 0 for the number 4
You could look at a step dictionary which can be used in many places:
q)(`s#-0W 3 6!-1 0 1) til 10
-1 -1 -1 0 0 0 1 1 1 1
Or fill the base case:
q)-1^(`s#3 6!0 1) til 10
-1 -1 -1 0 0 0 1 1 1 1
Even better here for your exact question is to go direct to using bin:
q)3 6 bin til 10
-1 -1 -1 0 0 0 1 1 1 1
Another option:
q){$[x<3;-1;x>5;1;0]}each til 10
-1 -1 -1 0 0 0 1 1 1 1
As an aside Sean and Rian's answers using bin are faster than using conditional evaluation - however in this context its already "fast". For readability's sake I would select the conditional evaluation due to how clear and easy it is to read, understand and modify the conditions and returns.
q)\t:10000 {[range;x] (range + 1 0) bin x}[3 5;-50+til 100]
11
q)\t:10000 3 6 bin -50+til 100
10
q)\t:10000 {$[x<3;-1;x>5;1;0]}'[-50+til 100]
75

How to create an array according to row and column number [duplicate]

This question already has answers here:
How can I change the values of multiple points in a matrix?
(3 answers)
Closed 6 years ago.
I want to create an array according to row number and column number.
For example :
row_number Column_number Value
1 1 5
3 2 10
4 6 4
7 5 66
The array should look like :
A=
5 0 0 0 0 0
0 0 0 0 0 0
0 10 0 0 0 0
0 0 0 0 0 4
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 66 0
Otherwise it will print zero.
3 possible methods to this regularly asked question:
1. Using the sub2ind function
A = zeros(max(row_number), max(Column_number));
idx = sub2ind(size(A),row_number, Column_number);
A(idx) = Value;
2. Calculating the linear indices manually
A = zeros(max(row_number), max(Column_number));
idx = row_number(:,1) + (Column_number(:,2)-1)*size(A,1)
A(idx) = Value;
3. Use a sparse matrix
sparse(row_number, Column_number, Value)
And then call full on that if you want to convert it to a regular matrix

Matlab adding rows and columns elegantly

Say that we have the following random matrix:
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2
I'd like to transform it into the following:
1 0 2 0 3 0 4 0
0 0 0 0 0 0 0 0
5 0 6 0 7 0 8 0
0 0 0 0 0 0 0 0
9 0 8 0 7 0 6 0
0 0 0 0 0 0 0 0
5 0 4 0 3 0 2 0
0 0 0 0 0 0 0 0
For some reason I cannot use mathjax format so it looks a bit awful, sorry for this. Point, is, that I want to add row and columns of zeros in between of my current rows and columns so that I increase its size 2x.
I came up with the following code, but it only works for very small matrixes if i use it on a a big image it cannot finish due to memory limitation problems.
clear all
I=imread('image.png');
I=rgb2gray(I);
B=zeros(2*size(I));
[x, y]=find(-inf<I<inf);
xy=[x,y];
nxy=xy;
%coord change
nxy=2*xy-1;
B(nxy(:,1),nxy(:,2))=I(xy(:,1),xy(:,2));
I expected to be fast because it is fully vectorised with maltlab functions but it fails miserably. Is there some other elegant way to do this?
If you take a look at your indexing vectors, this is something like I([1 1 2 2] ,[1 2 1 2] ); for a 2x2 matrix which means you index each row and column twice. The right solution is B(1:2:end,1:2:end)=I; which indexes every second row and every second column.
This can be also done via the one liner, say your original matrix is called A, then
kron(A,[1,0;0,0])

make a delay in my vector, matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have vector for example. Ones and zeros are representing traffic and idle states for base station traffic.
u=[1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ]
I know how to calculate number of ones and zeros in it after each break.
But I need help for delay. Zeros are telling me when I can put my base station in sleep mode.
Is there a way to make a delay for example that, base station does not go to sleep after first zero, instead of that that it goes to sleep after third zero, that means with some kind o delay.
Not a simple solution but yet I think you should find it interesting. You can optimize from here.
First of all I assumed you wanted to go to sleep after the third consecutive 0.
Check the example.
CODE:
u=[1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ];
bw_u=bwlabel(u==0);
get_breaks=bsxfun(#eq, bw_u, unique(bw_u)');
pos_break=cumsum(get_breaks,2).*get_breaks;
third_0=pos_break(2:end,:)==3;
[~,indx_third_0]=find(third_0)
OUTPUT:
indx_third_0 =
8
20
32
43
Again assuming ASantosRibeiro interpreted your question correctly, you can do it a little more simply as follows. Let n denote the desired number of zeros. In your case, n=3;
>> ind = find(diff(conv(2*u-1, repmat(-1, [1 n])) == n) == 1) + 1
ind =
8 20 32 43
Assuming ASantosRibeiro interpreted your question correctly, you could do this to find when to "go to sleep":
delay = 3;
idx = find(diff(u)==-1) + delay
idx =
8 20 32 43
This is assuming you want a delay of 3.
diff(u) returns the difference between consecutive elements in u. Finding when the difference equals -1 is equivalent to finding the indices of when the light changes from 1 to 0. By adding the delay, you have your desired indices.

Logical operations on matrices columns.

Let say that I have 1 matrix with numbers (0,1). How can i create new matrix that is the result of a logical operation among the columns?
eg. A =
0 0 0 1 0
1 1 1 1 1
0 1 1 0 0
0 0 0 0 1
1 0 0 1 0
1 1 1 1 1
If all elements of **rows** are equal to 1 - 1, if not - 0.
(like AND operation)
Ans= 0
1
0
0
0
1
Thanks!
To solve your problem this would work -
all(A,2)
If you were looking to set elements based on the columnwise data in A, you would do this -
all(A,1)
More info on all, must serve you well.