MATLAB: How to find subgroup min or max find subgroup min group_id=[1 1 1 2 2 3 3 3]'; B=[ 5 6 7 4 1 2 2 3]';output: C=[ 5 0 0 0 1 2 2 0]'; Best Answer Use accumarray and bsxfun:X = [1;1;1;2;2;3;3;3];B = [5;6;7;4;1;2;2;3];A = accumarray(X,B,[],@min);Y = any(bsxfun(@eq,A,B.'));C = zeros(size(B));C(Y) = B(Y)where the output isC = 5 0 0 0 1 2 2 0 Related SolutionsMATLAB: Divide group into subgroups group=[2 1 2 3 5]the variable group indicate what you want MATLAB: Legend plot of subgroup lines % code figure(2) hold on; tot_f=1; for k=1:3:(size(Data_dim,2)) color_plot(tot_f,:)=[rand(1),rand(1),rand(1)]; h{tot_f}=plot(time, Y(:,k:k+2),'Color', color_plot(tot_f,:)); desired_legends{tot_f} = ... some appropriate string xlabel('Time (s)') ylabel('NU') ax=gca; ax.YLim=[0 50]; ax.XLim=[0 100]; tot_f=tot_f+1; end hold off; group_handles = cellfun(@(V) V(1), h); %get first out of each group legend(group_handles, desired_legends) Related QuestionHow to change Elements in Matrix per row depending on their size in reference to a particular Element (without loop)Sort cell array columns by two dimensionsHow could I cut a matrix into subgroupsFind common non-zero indexesHow to assign same value according to each subgroup
Best Answer