Create a matrix that contains a specific total value, made by the maximum values, of another matrix

2 次查看(过去 30 天)
So, I have an NxM matrix of doubles that is asymmetrically arranged. What I'd like to do is create a second matrix that contains a specific percentage of the total of the first matrix at indexes that match the location of the points in the first matrix, and have the second matrix be zero everywhere else.
I have written code that can do this, but its painfully slow. I'm hoping someone might have some ideas on how to make this more efficient. The code I have written is below:
temp_array=original_array; % copy original array
ratio=0.6; % desired percentage total of original array
len=length(original_array); hgt=height(original_array); % Create empty array
dummy=zeros(len, hgt);
total=ratio*sum(sum(original_array));
running_tot=0;
while running_tot<total
temp_val = max(max(temp_array)); % Find max value of input
[y_val, x_val]=find(temp_array==temp_val); % Pull indicies
dummy(y_val, x_val)=temp_val; % Put max value in dummy array
temp_array(y_val, x_val)=0; % Delete max value
running_tot=running_tot+temp_val;
end
This preserves the original array and contains the relevant data in the array titled "dummy." But it's egregiously slow - I'd like to make it faster. I've considered reversing the way the loop works and deleting the minimum values rather than keeping the maximum values, but there's no way to tell if the min(min(temp_matrix)>0, which would get the loop stuck.
Any thoughts on how I might go about increasing the efficency of this code? Maybe a matrix-based method that I haven't thought of?
Thanks!

采纳的回答

Matt J
Matt J 2021-8-9
编辑:Matt J 2021-8-9
temp_array=original_array; % copy original array
ratio=0.6; % desired percentage total of original array
total=ratio*sum(original_array(:));
u=unique(original_array);
idx=find(cumsum(u,'reverse')<total,1);
thresh=u(idx);
temp_array(temp_array>=thresh)=0;
dummy=original_array-temp_array;
  1 个评论
Matt
Matt 2021-8-9
That works perfectly! It took me a little while to work out
idx=find(cumsum(u,'reverse')<total,1);
thresh=u(idx);
but I think I've got it figured out. It probably would have taken me quite a while to work out using cumsum. Thanks for your help!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by