function to find max values for intervals
4 ビュー (過去 30 日間)
古いコメントを表示
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 .
0 件のコメント
採用された回答
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
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
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!