Finding the max sum of the sub arrays.
이전 댓글 표시
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
Jan
2014년 12월 5일
Do I understand correctly: You are not allowed to use for, subsref, subsasgn, colon, horzcat?
I do not see, how a contains subarrays of 4 elements. Which matrices are forbidden to use?
Adam
2014년 12월 5일
a, start and end are matrices so if you are forbidden to use matrices I would imagine the problem is impossible.
I assume this is a homework question though as they are the only places such stupid restrictions get made so isn't the idea to work out a solution by yourself in that case?!
Valeria
2014년 12월 5일
Valeria
2014년 12월 5일
채택된 답변
추가 답변 (3개)
Image Analyst
2014년 12월 5일
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.
Andrei Bobrov
2014년 12월 6일
편집: Andrei Bobrov
2014년 12월 6일
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
Image Analyst
2014년 12월 6일
편집: Image Analyst
2014년 12월 6일
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.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!