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일

0 개 추천

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

James Rodriguez
James Rodriguez 2019년 12월 27일
편집: James Rodriguez 2019년 12월 27일
Thanks for the reply. There may have been a misunderstanding here. I already have my vector(lets call it sf for short) (20418x1) and I need to find for it the maximum values from the interval pattern I mentioned. I am unfamilar with some of the coding used here and would appreciate guidance and demonstation on how to adjust it to work for the vector I have.
My pleasure.
I created the ‘x’ vector to test my code, and to demonstrate how to use the vector with my code. This is a common practice when the actual data are not provided. (It would have helped to have known the dimensions of your actual vector earlier.)
I used mean rather than max in my original code (I was concentrating on getting the code to run). The slightly corrected code, incorporating your actual vector (actually a test vector of that size that I do not include in this code):
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);
This variation ‘pads’ the ‘sf’ vector with a vector of -realmax (-1.797693134862316e+308), the largest negative values possible, to make it compatible with the reshape call, and to prevent the padded values from appearing as the maximum values unless they are the only values. Eliminate them from the ‘Out’ vector with:
Out = Out(Out > -realmax);
to get the final result.
Image Analyst
Image Analyst 2019년 12월 27일
Your pattern length is 605 = 152 + 3*151. However your array sf is not a multiple of the 605 pattern length. Do you know that?
Star Strider
Star Strider 2019년 12월 27일
@Image Analyst — Yes. My slightly revised code corrects for that.
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일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2019년 12월 26일

댓글:

2019년 12월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by