Maximum Subarray variation - average

I have to solve a problem much like the maximum subarray problem. I have to find the largest subarray whose average is bigger than k. I thought the following trick. I can transform my array A[] of size n to a B[] where B[i] = A[i] - k. So now the average must be >0. But average greater than zero doesnt simply mean sum greater than zero? So I can directly apply Kadane's algorithm. Am I right? (always under the constraint that there is 1 positive value)

no, kadane's algorithm will still find you the subarray with the biggest sum...i have to solve the same problem. so far i kave find that if we create the array B as you mentioned above and then make the array C which contains the partial sums of the array B,then the maximum interval (i,j) that we are lookink for has the same number for i and j!!! for example:
array A is: 1 10 -1 -1 4 -1 7 2 8 1 .....and the given k is 5 then
array B is: -4 5 -6 -6 -1 -6 2 -3 3 -4
array C is:-4 1 -5 -11 -12 -18 -16 -19 -16 -20
so the subarray that we are looking for is [7,2,8], has length 3, and has the same first and last element which is -16!!!!
edit: i forgot to tell that we are searching for a O(n) or an O(n*logn) algorithm.... #lets_solve_it you are right but your algorithm is O(n^2) whitch is way to big for the data we want to handle. i 'm close to solve it with the function map in c++,whitch is something like a hash table. i thing this is the right diredtion because here the elements of the array C have direct relation with their indexes! Also our professor told us that another possible solution ,is to make again the array C and then take a (special?) pivot to do quicksort....but i don't totally understand what we expect from quicksort to do.

#panos7:
after you have created array C (partial sums array), you seek two values of C, Ci and Cj,
such that, Cj>=Ci, and, (j-i) is as "big" as possible. (j-i) --> MAX.
then return j-i.
in your example, -16>=-18 so you returned j-i=9-6=3
which is the correct answer!

Related

Compare different sized vectors with different values

I'm still fairly new to working with Matlab and programming. I have a dataset with n trials, of these (in this case) m are relevant. So I have an m-by-1 vector with the indices of the relevant trials (rel). I have another vector (Correct which is n-by-1) that consists of 0 and 1. n is always bigger than m. I need to know which trials (of the m-by-1 relevant trials) have a 1 in the n-by-1 vector. I have tried for-loops but I always get an error 'Index exceeds matrix dimensions.'
Here is my code:
for i=1:length(rel);
CC=rel(find(Correct==1));
end;
I think it should be fairly simple but I don't know yet how to explain to Matlab what I want...
Thank you all for your answers. I realized that my question was not as clear as I thought (also a learning process I guess..) so your suggestions weren't exactly what I need. I'm sorry for being unclear.
Correct is not a logical, it does contain 0 and 1 but these refer to correct or incorrect answer (I'm actually not sure if this matters but I thought I let you know)
rel is a subset of the original data with all trials (all trials=n trials), Correct is the same length as the original vector with all trials (n trials). So rel contains the indices of the (for me) relevant trials of the original data and is that way connected to Correct.
I hope this makes my question a bit more clear, if not, let me know!
Thank you!
It's not quite clear from your question what you are trying to do but I think I have an idea.
You have a vector n similar to
>> n = round(rand(1, 10))
n =
0 1 1 0 0 0 1 0 0 1
and m is indices of this vector similar to
>> m = [1 3 7 9];
Now we use m to index n as n(m) which will return the values of n corresponding to the elements in m. Next we need to check these for equality with 1 as n(m) == 1 and finally we need to figure out what values of m have n equal to 1 again by indexing. So putting this altogether we get
>> m(n(m) == 1)
ans =
3 7
To find the indices of m that are being returned you can use
>> find(n(m) == 1)
ans =
2 4
I'm assuming that Correct is of type logical (i.e. it contains trues and falses instead of 0s and 1s).
You actually don't need a loop here (this is clear in your case, since you are looping over i and never actually use i in your loop):
m = numel(rel)
CC = rel(Correct(1:m))
The reason you are getting that error is because Correct has more elements than rel so you are attempting to address elements beyond the end of rel. I solve this above by only considering the first m elements of Correct.

MATLAB Multiple Maximum Values

I am having problem with MATLAB's maximum function. What I am supposed to do is to replace the maximum value of an array with a number. However, when there are more than one maximal value, the program updates all of them simultaneously. Is there a way to make it do it one by one? The order of replacement is not important; it can be done arbitrarily. The only important thing is to have MATLAB doing it one by one.
Thank you in advance.
The second output of max returns one index:
a=[5,5];
[b,idx]=max(a)
c=b-2;
a(idx)=c
When you say more than 1 maximum value, I assume you are talking about a matrix where max function operates on every column?
You can do the following:
a = [1 1 2;5 5 7; 3 2 9]
Obviously, the maximum value is going to be 9, but if you do the following:
max(a)
The result will be:
5 5 9
Based on each column.
The following may work for you?
max(a(:)) % Maximum value from a matrix (rerranged into 1 column)
you can do the same for the min function.

how to sum matrix entities as indexing by another vector values?

Suppose I have a vector B=[1 1 2 2] and A=[5 6 7 4] in the form of B says the numbers in the A that are need to be summed up. That is we need to sum 5 and 6 as the first entry of the result array and sum 7 and 4 as the second entry. If B is [1 2 1 2] then first element of the result is 5+7 and second element is 6+4.
How could I do it in Matlab in generic sense?
A fexible and general approach would be to use accumarray().
accumarray(B',A')
The function accumulates the values in A into the positions specified by B.
Since the documentation is not simple to understand I will summarize why it is flexible. You can:
choose your accumulating function (sum by default)
specify the positions as a set of coordinates for accumulation into ND arrays
preset the dimension of the accumulated array (by default it expands to max position)
pad with custom values the non accumulated positions (pads with 0 by default)
set the accumulated array to sparse, thus potential avoiding out of memory
[sum(A(1:2:end));sum(A(2:2:end))]

Subselecting matrix and use logical selection (matlab)

I have a line of code in matlab for which i am selecting a subset of a matrix:
A(3:5,1:3);
Now i want to adapt this line, to only select rows for which all three values are larger than zero:
(A(3:5,1:3) > 0);
But apparently i am not doing this right. How do i select part of the matrix, and also make sure that only the rows (for which all three values are) larger than zero are selected?
EDIT: To clarify: lets say that i have a matrix of coordinates called A, that looks like this:
Matrix A [5,3]
3 4 0
0 1 0
0 3 1
0 0 0
4 8 7
Now i want to select only part [3:5,1:3], and of that part i only want to select row 3 and 5. How do i do that?
The expression:
A(find(sum(A(3:5,:),2)~=0),:)
will return only the rows of A(3:5,:) which have a row-sum not equal to zero.
If you had posted syntactically correct Matlab it would have been easier for me to cut and paste your test data into my Matlab session.
I'm modelling this answer off of A(find( A > 0 ))
distances = pdist(find( pdist(medoidContainer(i,1:3)) > 0 ));
This will give you a vector of values in the distances variable. The reason the pdist(medoidContainer(i,1:3) > 0) does not work is because it first, finds the indices specified by i,1:3 in medoidContainer. Then it finds the indices in medoidContainer(i,1:3) that are greater than 0. However, since medoidContainer(i,1:3) and pdist now likely have different dimensions, the comparison does not give the right indexes.

Linear Probing in Open Addressing

I have an array with size m = 11 and my hash function is Division method : h(k) = k mod m
I have an integer k = 10 and 10 mod 11 is -1 so where should I put this key in the array? I should put this key in the slot which its index is 10?
please help me thanks
EDITED : for getting my answer well for example I have integers like k = 10,22,31,4,15,28,17,88,59
the array would be like this?thanks
10 9 8 7 6 5 4 3 2 1 0 index
10 31 59 17 28 4 15 88 22 keys
As it's usually done, 10 mod 11 is 10, so yes, you'd normally use index 10.
Edit: To generalize: at least as it's normally defined, given two positive inputs, a modulo will always produce a positive result. As such, your questions about what to do with negative results don't really make sense with respect to the normal definition.
If you really do have the possibility of getting a negative result, my immediate reaction would be to switch to some language that will produce a reasonable result. If you can't do that, then you'd probably want to move the value into the correct range by adding m to the negative number until you get a number in the range [0..m) so it fits the normal definition of mod, then use that as your index.