Matlab: Increasing Matrix Values ​​Using Indexes

I have an index vector and you want to increase the values ​​in the matrix in each index. For instance:

    ind = [1 2 2 5];
    m = zeros(3);
    m(ind) = m(ind) + 1;

The result is as follows:

    m = [1 0 0
         1 1 0
         0 0 0]

But I need results

    m = [1 0 0
         2 1 0
         0 0 0]

Temporary complexity is very important to me, and I cannot use it. Thank.

+4
source share
3 answers

For a sorted array of indices, we can have a game diff-

out = zeros(M,N);  % Output array of size(M,N)
df = diff([0,ind,ind(end)+1]);
put_idx = diff(find(df)); % gets count of dups
out(ind(df(1:end-1)~=0)) = put_idx;

- diff. , . , , , .

Script (create_data.m) -

function ind = create_data(M,N, num_unq_ind, max_repeats)

unq_ind = unique(randi([1,M*N],1,num_unq_ind));
num_repeats = randi(max_repeats, [1,numel(unq_ind)]);
ind = repelem(unq_ind, num_repeats);

script (bench1.m) -

clear all; close all;

M = 5000; % Array size
N = 5000;

% Input params and setup input indices array (edited for various runs)
num_unq_ind = 100000;
max_repeats = 100;
ind = create_data(M,N, num_unq_ind, max_repeats);

num_iter = 100; % No. of iterations to have reliable benchmarking 
disp('Input params :')
disp(['num_unq_ind = ' int2str(num_unq_ind)])
disp(['max_repeats = ' int2str(max_repeats)])

disp('------------------ Using diff ----------------')
tic
for i=1:num_iter
    out = zeros(M,N);
    df = diff([0,ind,ind(end)+1]);
    put_idx = diff(find(df));
    out(ind(df(1:end-1)~=0)) = put_idx;
end
toc

% Luis soln
disp('------------------ Using accumaray ----------------')
tic
for i=1:num_iter
    m = reshape(accumarray(ind(:), 1, [N^2 1]), N, N);
end
toc

-

>> bench1
Input params :
num_unq_ind = 10000
max_repeats = 10
------------------ Using diff ----------------
Elapsed time is 0.948544 seconds.
------------------ Using accumaray ----------------
Elapsed time is 1.502658 seconds.
>> bench1
Input params :
num_unq_ind = 100000
max_repeats = 10
------------------ Using diff ----------------
Elapsed time is 1.784576 seconds.
------------------ Using accumaray ----------------
Elapsed time is 1.533280 seconds.
>> bench1
Input params :
num_unq_ind = 10000
max_repeats = 100
------------------ Using diff ----------------
Elapsed time is 1.315998 seconds.
------------------ Using accumaray ----------------
Elapsed time is 1.391323 seconds.
>> bench1
Input params :
num_unq_ind = 100000
max_repeats = 100
------------------ Using diff ----------------
Elapsed time is 6.180565 seconds.
------------------ Using accumaray ----------------
Elapsed time is 3.576154 seconds.

sparsey , accumarray, , .

+4

. .

ind = [1 2 2 5];
N = 3;
m = full(reshape(sparse(ind, 1, 1, N^2, 1), N, N));

,

ind = [1 2 2 5];
N = 3;
m = reshape(accumarray(ind(:), 1, [N^2 1]), N, N);

( @beaker)

ind = [1 2 2 5];
N = 3;
m = zeros(N);
m(:) = accumarray(ind(:), 1, [N^2 1]);

, , :

ind = [1 2 2 5];
N = 3;
m = zeros(N);
[ii, ~, vv] = find(accumarray(ind(:), 1));
m(ii) = vv;
+6

histcounts

n = 3;
m = reshape(histcounts(ind, [1:n^2 n^2]), n, n);
+3

Source: https://habr.com/ru/post/1684909/


All Articles