필터 지우기
필터 지우기

function to find max values for intervals

조회 수: 8 (최근 30일)
James Rodriguez
James Rodriguez 2019년 12월 26일
댓글: Star Strider 2019년 12월 27일
I have a single column with many values . I need to find the maximum value for the first 152 numbers. I need matlab to also output the max for the next 151, then the next 151 as well as the next 151. Then I need the max for the next 152 and then the next 151 and this same pattern repeats for all the data. The alternating 152 is what makes this task seemingly impossible .

채택된 답변

Star Strider
Star Strider 2019년 12월 26일
편집: Star Strider 2019년 12월 27일
Try this:
x = sf; % Set ‘x’ = ‘sf’
collen = 605; % Column Length (Incorporating All Sub-Sections)
xtra = rem(numel(x),collen); % Elements Beyond 605*N Segments
lenpad = ones(collen-xtra,1)*(-realmax); % Pad With Largest Negative Values (Will Not Be ‘max’ Of Other Elements)
xc = [x; lenpad]; % Append To Existing ‘x’ To Create ‘xc’
xr = reshape(xc, collen, []);
parts = [152 [1 1 1]*151];
csparts = cumsum(parts);
idxvct = [0 csparts];
for k = 1:numel(idxvct)-1
IdxCheck = [idxvct(k)+1 idxvct(k+1)] % Index Check (Delete)
idxrng = idxvct(k)+1:idxvct(k+1);
maxmtx{k,:} = max(xr(idxrng,:),[],1);
end
Out = reshape(cell2mat(maxmtx), [],1);
Out = Out(Out > -realmax);
The loop is necessary because of the differing lengths. The code creates a matrix from the initial vector, then operates on it column-wise to get the means, then uses the cell2mat and reshsape functions to create the column output corresponding to the original column vector.
The ‘IdxCheck’ vector simply reports the index range for that iteration of the loop. It is not necessary for the code. I posted it so you can be certain it partitions the vector as you want it to.
  댓글 수: 6
Malik Khan
Malik Khan 2019년 12월 27일
This works splendidly. I deleted check to keep things compact. But thanks so much. If you can change the original comment to this I'll make this accepted answer
Star Strider
Star Strider 2019년 12월 27일
Thank you!
I am not certain what you mean by ‘change the original comment’. I copied my revised code to my original Answer, replacing the code I originally posted, and slightly edited the other text. (I am keeping my later code and explanatory text as well in that Comment, so as not to disrupt the flow of the thread.)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 12월 27일
It's not compact or cryptic but this brute force method is pretty easy to follow and understand.
r = rand(20418, 1); % Generate sample data.
counter = 1;
lenr = length(r)
for row = 1 : (152 + 3*151) : length(r)
row1 = row;
row2 = row1 + 151;
if row2 > lenr
break; % Quit if it would be beyond the end of the array.
end
fprintf('Getting means between row %d and %d, inclusive.\n', row1, row2);
theMeans(counter) = mean(r(row1:row2));
row1 = row2 + 1;
row2 = row1 + 150;
if row2 > lenr
break; % Quit if it would be beyond the end of the array.
end
fprintf('Getting means between row %d and %d, inclusive.\n', row1, row2);
theMeans(counter + 1) = mean(r(row1:row2));
row1 = row2 + 1;
row2 = row1 + 150;
if row2 > lenr
break; % Quit if it would be beyond the end of the array.
end
fprintf('Getting means between row %d and %d, inclusive.\n', row1, row2);
theMeans(counter + 2) = mean(r(row1:row2));
row1 = row2 + 1;
row2 = row1 + 150;
if row2 > lenr
break; % Quit if it would be beyond the end of the array.
end
fprintf('Getting means between row %d and %d, inclusive.\n', row1, row2);
theMeans(counter + 3) = mean(r(row1:row2));
counter = counter + 4;
end

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by