Filter löschen
Filter löschen

Finding the max sum of the sub arrays.

5 Ansichten (letzte 30 Tage)
Valeria
Valeria am 5 Dez. 2014
Kommentiert: Image Analyst am 6 Dez. 2014
Having a vector a, that contains sub arrays of 4 elements (their amount is unknown), knowing their starting indices in a and ending indices in a in two vectors=
start=[y1 y2 y3.....]
end=[x1 x2 x3.....]
(yet, we don't know how much sub arrays we get, so the length of the start/end vector is unknown, but they are equal). We need to create the vector of the sums like following: sums=[sum(a((y1):(x1))) sum(a((y2):(x2))) ....] and then find the max sum (that's an easy part).
for example:
a=[7 2 6 14 30 15 12 0 20 21 300 67]
start=[2 8]
end=[5 11]
sums=[52 341]
How can we write the code for the general problem?
Only these functions are allowed :
min , max , sum , find , any , all , isempty , sort, length
I can't figure it out without using the matrices, which is forbidden. Any ideas?
  4 Kommentare
Valeria
Valeria am 5 Dez. 2014
Not allowed. All multiple dimensions arrays are forbidden, only vectors. That doesn't matter what are the requirements for the subarrays.
The question is, knowing the starting and the ending points of them, how do I sum them to the new vector?
Valeria
Valeria am 5 Dez. 2014
Adam, we can use arrays. Or, if you insist, the matrix with one row only. I've been working on this question for 2 days, well, it's the last and the smallest part of it, the rest is done. But this part I just can't complete.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Valeria
Valeria am 6 Dez. 2014
I managed. We create two help arrays: ind2=start+1; ind3=start+2;
So the sum vector will be: sums=start+ind2+ind3+end;
Thank you all for your assistance!!
  1 Kommentar
Image Analyst
Image Analyst am 6 Dez. 2014
You managed to create an error, not solve the problem. Even if you fix the variable names, it's not right because you're not even involving the "a" in any way. Here's your code
a=[7 2 6 14 30 15 12 0 20 21 300 67]
starting=[2 8]
ending=[5 11]
desiredSums=[52 341] % What you want to get
% Now your code:
% Create two helper arrays
ind2=starting+1;
ind3=starting+2;
% So the sum vector will be:
sums=starting+ind2+ind3+ending
And here's the output
desiredSums =
52 341
sums =
14 38
As you can see, your sums do not produce the desired result.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Image Analyst
Image Analyst am 5 Dez. 2014
Valeria: You simply need to give an index to the sums vector and put that index into the "a" array inside the sum function.
a=[7 2 6 14 30 15 12 0 20 21 300 67]
startingIndexes = [2, 8]
endingIndexes = [5, 11]
for k = 1 : length(startingIndexes)
sums(k) = sum(a( you do this part) )
end
Inside you simply need to give the starting and ending indexes as a function of k. Trivial - I think you can do that part.
  1 Kommentar
Valeria
Valeria am 5 Dez. 2014
Unfortunately, loops are forbidden to use too. Only basic matlab functions.

Melden Sie sich an, um zu kommentieren.


Andrei Bobrov
Andrei Bobrov am 6 Dez. 2014
Bearbeitet: Andrei Bobrov am 6 Dez. 2014
a=[7 2 6 14 30 15 12 0 20 21 300 67];
start=[2 8];
end1 =[5 11];
a1 = cumsum(a);
out = diff(a1([start;end1]))+a(start);
  1 Kommentar
Image Analyst
Image Analyst am 6 Dez. 2014
Bearbeitet: Image Analyst am 6 Dez. 2014
She already told Matt that she could not use the cumsum() function. And told me that she can't use loops (so that makes it almost impossible to do a cumsum manually). Apparently her professor found a trick where it is possible to get the answer using only the functions min , max , sum , find , any , all , isempty , sort, length , and no matrices (2-D arrays) and no loops of any kind . I'm stumped.

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 5 Dez. 2014
Hint - consider the CUMSUM command.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by