logo
down
shadow

Max within groups in Matlab


Max within groups in Matlab

By : sidb
Date : November 17 2020, 11:58 AM
it helps some times I have the following matrix: , accumarray does exactly what you want
code :
x=[ 2 5 7 8 1 3 4 6 5 7 3 1; 1 1 1 1 2 2 2 2 3 3 3 3;]
accumarray(x(2,:)',x(1,:)',[],@max)


Share : facebook icon twitter icon
MATLAB Legend Groups

MATLAB Legend Groups


By : user3290476
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You may want to have a look at LEGENDFLEX that you can download from the Matlab File Exchange
Matlab: Plotting bar groups

Matlab: Plotting bar groups


By : Parwez
Date : March 29 2020, 07:55 AM
will help you I got the way to achieve group bars:
I had to plot the data such that there are 8 groups of bars where each group consists of 3 bars.
code :
Y = [30.9858    1.36816 38.6943
    0.655176    6.44236 13.1563
    1.42942     3.0947  0.621403
     22.6364    2.80378 17.1299
    0.621871    5.37145 1.87824
    0.876739    5.97647 3.80334
     40.6585    68.6757 23.0408
     2.13606    6.26739 1.67559

    ];
bar(Y)
Split data in 2 groups in Matlab with both groups having all unique ids

Split data in 2 groups in Matlab with both groups having all unique ids


By : Lesley
Date : March 29 2020, 07:55 AM
will help you I am trying to split a data set into 2 groups such that both groups have all unique ids present at least once. The data set is something like , script
code :
clear;clc

A = [01 02 03 04 05 06 07;
07 05 08 09 10 11 12;
01 04 07 13 08 14 15;
06 10 11 12 08 01 02;
13 14 10 01 07 03 02;
15 01 03 04 10 13 11;
11 12 03 05 07 14 15;
06 05 10 13 01 09 14];

% find unique elements and rows containing them
UniqElem = unique(A);
NUniqElem = length(UniqElem);
UniqIndex = struct('UniqElem', cell(NUniqElem,1), ...
    'UniqRows', cell(NUniqElem, 1), 'RowCount', cell(NUniqElem, 1));
for ii = 1:NUniqElem
    t1 = UniqElem(ii);
    t2 = find(any(A==t1,2));
    UniqIndex(ii).UniqRows = t2;
    UniqIndex(ii).UniqElem = t1;
    UniqIndex(ii).RowCount = length(t2);
end
clear('t1','t2')

% find all possible combinations to make the first group
Combs1 = testf(UniqIndex);
Combs2 = struct('Combination', Combs1, ...
    'Unique', {true});
for ii = 1:(length(Combs2)-1)
    if Combs2(ii).Unique
        CurrentComb = Combs2(ii).Combination;
        for jj = (ii+1):length(Combs2)
            if Combs2(jj).Unique && ...
                    all(ismember(CurrentComb,Combs2(jj).Combination))
                Combs2(jj).Unique = false;
            end
        end
    end
end
Combs3 = Combs2([Combs2.Unique]);
Combs4 = struct('Grp1', {Combs3.Combination}, 'Grp2', []);
AllRows = 1:size(A,1);
for ii = 1:length(Combs4)
    Combs4(ii).Grp2 = AllRows(~ismember(AllRows, Combs4(ii).Grp1));
end
Combs5 = struct('Grp1', [], 'Grp2', []);
for ii = 1:length(Combs4)
    if all(ismember(UniqElem, unique(A([Combs4(ii).Grp2], :))))
        Combs5(end+1) = Combs4(ii);
    end
end

Combinations = Combs5;
for ii = 1:length(Combinations)
    fprintf('Solution %d of %d \n', ii, length(Combinations))
    CurrentComb = Combinations(ii);
    fprintf('Group 1 \n')
    for jj = 1:length(CurrentComb.Grp1)
        fprintf('R%2d: %s \n', CurrentComb.Grp1(jj), ...
            num2str(A(CurrentComb.Grp1(jj), :), '%-4d') )
    end
    fprintf('Group 2 \n')
    for jj = 1:length(CurrentComb.Grp2)
        fprintf('R%2d: %s \n', CurrentComb.Grp2(jj), ...
            num2str(A(CurrentComb.Grp2(jj), :), '%-4d') )
    end
    fprintf('\n')
end
function Comb = testf(UniqRowIn)
if length(UniqRowIn) == 1
    Comb = num2cell(UniqRowIn.UniqRows)';
else
    t2 = testf(UniqRowIn(2:end));
    t1 = UniqRowIn(1).UniqRows;
    Comb = cell(0);
    for ii = 1:length(t2)
        CurrentComb = t2{ii};
        if isempty(intersect(CurrentComb, t1))
            for jj = 1:length(t1)
                Comb{end+1,1} = sort([CurrentComb, t1(jj)]);
            end
        else
            Comb{end+1,1} = CurrentComb;
        end
    end
end
end
Solution 1 of 12 
Group 1 
Group 2 

Solution 2 of 12 
Group 1 
R 1: 1   2   3   4   5   6   7 
R 2: 7   5   8   9   10  11  12 
R 3: 1   4   7   13  8   14  15 
Group 2 
R 4: 6   10  11  12  8   1   2 
R 5: 13  14  10  1   7   3   2 
R 6: 15  1   3   4   10  13  11 
R 7: 11  12  3   5   7   14  15 
R 8: 6   5   10  13  1   9   14 

Solution 3 of 12 
Group 1 
R 3: 1   4   7   13  8   14  15 
R 4: 6   10  11  12  8   1   2 
R 5: 13  14  10  1   7   3   2 
R 8: 6   5   10  13  1   9   14 
Group 2 
R 1: 1   2   3   4   5   6   7 
R 2: 7   5   8   9   10  11  12 
R 6: 15  1   3   4   10  13  11 
R 7: 11  12  3   5   7   14  15 

Solution 4 of 12 
Group 1 
R 3: 1   4   7   13  8   14  15 
R 4: 6   10  11  12  8   1   2 
R 6: 15  1   3   4   10  13  11 
R 8: 6   5   10  13  1   9   14 
Group 2 
R 1: 1   2   3   4   5   6   7 
R 2: 7   5   8   9   10  11  12 
R 5: 13  14  10  1   7   3   2 
R 7: 11  12  3   5   7   14  15 

Solution 5 of 12 
Group 1 
R 3: 1   4   7   13  8   14  15 
R 4: 6   10  11  12  8   1   2 
R 7: 11  12  3   5   7   14  15 
R 8: 6   5   10  13  1   9   14 
Group 2 
R 1: 1   2   3   4   5   6   7 
R 2: 7   5   8   9   10  11  12 
R 5: 13  14  10  1   7   3   2 
R 6: 15  1   3   4   10  13  11 

Solution 6 of 12 
Group 1 
R 1: 1   2   3   4   5   6   7 
R 3: 1   4   7   13  8   14  15 
R 7: 11  12  3   5   7   14  15 
R 8: 6   5   10  13  1   9   14 
Group 2 
R 2: 7   5   8   9   10  11  12 
R 4: 6   10  11  12  8   1   2 
R 5: 13  14  10  1   7   3   2 
R 6: 15  1   3   4   10  13  11 

Solution 7 of 12 
Group 1 
R 1: 1   2   3   4   5   6   7 
R 2: 7   5   8   9   10  11  12 
R 5: 13  14  10  1   7   3   2 
R 6: 15  1   3   4   10  13  11 
Group 2 
R 3: 1   4   7   13  8   14  15 
R 4: 6   10  11  12  8   1   2 
R 7: 11  12  3   5   7   14  15 
R 8: 6   5   10  13  1   9   14 

Solution 8 of 12 
Group 1 
R 2: 7   5   8   9   10  11  12 
R 4: 6   10  11  12  8   1   2 
R 5: 13  14  10  1   7   3   2 
R 6: 15  1   3   4   10  13  11 
Group 2 
R 1: 1   2   3   4   5   6   7 
R 3: 1   4   7   13  8   14  15 
R 7: 11  12  3   5   7   14  15 
R 8: 6   5   10  13  1   9   14 

Solution 9 of 12 
Group 1 
R 4: 6   10  11  12  8   1   2 
R 5: 13  14  10  1   7   3   2 
R 6: 15  1   3   4   10  13  11 
R 8: 6   5   10  13  1   9   14 
Group 2 
R 1: 1   2   3   4   5   6   7 
R 2: 7   5   8   9   10  11  12 
R 3: 1   4   7   13  8   14  15 
R 7: 11  12  3   5   7   14  15 

Solution 10 of 12 
Group 1 
R 1: 1   2   3   4   5   6   7 
R 2: 7   5   8   9   10  11  12 
R 6: 15  1   3   4   10  13  11 
R 7: 11  12  3   5   7   14  15 
Group 2 
R 3: 1   4   7   13  8   14  15 
R 4: 6   10  11  12  8   1   2 
R 5: 13  14  10  1   7   3   2 
R 8: 6   5   10  13  1   9   14 

Solution 11 of 12 
Group 1 
R 4: 6   10  11  12  8   1   2 
R 6: 15  1   3   4   10  13  11 
R 7: 11  12  3   5   7   14  15 
R 8: 6   5   10  13  1   9   14 
Group 2 
R 1: 1   2   3   4   5   6   7 
R 2: 7   5   8   9   10  11  12 
R 3: 1   4   7   13  8   14  15 
R 5: 13  14  10  1   7   3   2 

Solution 12 of 12 
Group 1 
R 1: 1   2   3   4   5   6   7 
R 2: 7   5   8   9   10  11  12 
R 5: 13  14  10  1   7   3   2 
R 7: 11  12  3   5   7   14  15 
Group 2 
R 3: 1   4   7   13  8   14  15 
R 4: 6   10  11  12  8   1   2 
R 6: 15  1   3   4   10  13  11 
R 8: 6   5   10  13  1   9   14 

>> 
Splitting Groups in MATLAB

Splitting Groups in MATLAB


By : HP9000
Date : March 29 2020, 07:55 AM
Hope that helps You can use accumarray to create this cell array for you. We first need to assign a unique value to each "group" of consecutive numbers which share a sign. We can then use accumarray to place all elements in a given group into an element of a cell array.
code :
A = cat(1, [-5 -5 5 5 -5 -5 -5 -5 5 5 5 5], [8 7 3 6 5 9 8 7 9 4 3 2 ]).';

% Compute the sign of each element: -1 for negative, 1 for positive
% Repeat first element for diff
S = sign(A([1 1:end],1));
%   -1    -1    -1     1     1    -1    -1    -1    -1     1     1     1     1

% Compute element-by-element differences
D = diff(S);
%   0     0     2     0     -2     0     0     0     2     0     0     0

% Convert to a logical matrix which will make any non-zero 1 and any zero stays 0
L = logical(D);
%   0     0     1     0     1     0     0     0     1     0     0     0

% Take the cumulative sum (and add 1) to give each group of elements a unique number
subs = cumsum(L) + 1;
%   1     1     2     2     3     3     3     3     4     4     4     4

% Use this as the first input to accumarray and perform a given action on all elements in 
% A(:,2) which share these values. Our action will be to convert to a cell array 
result = accumarray(subs, A(:,2), [], @(x){x}); 

%   result{1} =
%       8     7
%
%   result{2} =
%       3     6
%
%   result{3} =
%       5     9     8     7
%
%   result{4} =
%       9     4     3     2
accumarray(1 + cumsum(logical(diff(sign(A([1 1:end],1))))), A(:,2), [], @(x){x})
MATLAB index within groups

MATLAB index within groups


By : Niels Bril
Date : March 29 2020, 07:55 AM
Does that help Given a vector A of group numbers (such as the one returned by findgroups), how to return a vector B of the same length containing indices of elements within groups of A? , A solution using sort and accumarray:
code :
[s is]=sort(A);
idx = accumarray(s(:),1,[],@(x){1:numel(x)});
B(is)=[idx{:}];
p=regionprops(A,'PixelIdxList');
B = zeros(size(A));
for k = 1: numel(p)
    B(p(k).PixelIdxList) = 1:numel(p(k).PixelIdxList);
end
Related Posts Related Posts :
  • Powershell: Comparing multiple values in an array
  • Add Elements to nullable Vector
  • How to get elements larger than x in a given range?
  • How to call Array-valued Functions in fortran?
  • How do I create and pass string array to a sub in Excel VBA?
  • How can I auto-sort an array in Swift by property type?
  • Trying to test collisions with objects in array
  • Sort an array of arrays in Erlang
  • push into deeply nested array
  • Encapsulate strings in an array in PowerShell
  • Send Specific variables through socket.io to client with socket.send
  • when preparing for segue array contains no data (using swift)
  • How do you access the attributes of a class instance from inside an array? (Swift)
  • Array.Find and IndexOf for multiple elements that are exactly the same object
  • How to find array items that occur at least K times
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org