1
votes

I have a m-by-n matrix and I want to shift each row elements k no. of times (" one resultant matrix for each one shift so a total of k matrices corresponding to each row shifts ")(k can be different for different rows and 0<=k<=n) and want to index all the resultant matrices corresponding to each individual shift.

Eg: I have the matrix: [1 2 3 4; 5 6 7 8; 2 3 4 5]. Now, say, I want to shift row1 by 2 times (i.e. k=2 for row1) and row2 by 3times (i.e. k=3 for row2) and want to index all the shifted versions of matrices (It is similar to combinatorics of rows but with limited and diffeent no. of shifts to each row).

Can someone help to write up the code? (please help to write the general code but not for the example I mentioned here)

I found the following question useful to some extent, but it won't solve my problem as my problem looks like a special case of this problem:

Matlab: How to get all the possible different matrices by shifting it's rows (Update: each row has a different step)

2
Show us the expected output for the given input data?Divakar
matrix--1:[1 2 3 4; 5 6 7 8; 2 3 4 5].user3354360
This is the output matrix, which is the same as the input? Try a better example then? How many shifts are there for each row? Try explaining the output with the requirements? Please edit the question with these.Divakar
matrix--1:[1 2 3 4; 5 6 7 8; 2 3 4 5](original matrix), matrix--2:[4 1 2 3; 5 6 7 8; 2 3 4 5], matrix--3: [3 4 1 2; 5 6 7 8; 2 3 4 5](2 shifted versions of row1), matrix--4: [1 2 3 4; 8 5 6 7; 2 3 4 5], matrix--5: [4 1 2 3; 8 5 6 7; 2 3 4 5], matrix--6:[3 4 1 2; 8 5 6 7; 2 3 4 5], etc..(shifted versions of row1 by shifting row2 one time),...user3354360
Not really sure what you need, but check out doc circshift . Unless I misinterpreted your question you could use this in a loop.Dennis Jaheruddin

2 Answers

0
votes

See if this works for you -

%// Input m-by-n matrix
A = rand(2,5) %// Edit this to your data

%// Initialize shifts, k for each row. The number of elements would be m.
sr = [2 3];   %// Edit this to your data

[m,n] = size(A); %// Get size

%// Get all the shits in one go
sr_ind = arrayfun(@(x) 0:x,sr,'un',0);   %//'
shifts = allcomb(sr_ind{:},'matlab')';  %//'

for k1 = 1:size(shifts,2)

    %// Get shift to be used for each row for each iteration
    shift1 = shifts(:,k1);

    %// Get circularly shifted column indices
    t2 = mod(bsxfun(@minus,1:n,shift1),n);
    t2(t2==0) = n; 

    %// Get the linear indices and use them to index into input to get the output
    ind = bsxfun(@plus,[1:m]',(t2-1)*m);  %//'
    all_matrices = A(ind) %// outputs
end

Please note that this code uses MATLAB file-exchange code allcomb.

0
votes

If your problem in reality is not more complex than what you showed us, it can be done by a double loop. However, i don't like my solution, because you would need another nested loop for each row you want to shift. Also it generates all shift-combinations from your given k-numbers, so it has alot of overhead. But this can be a start:

% input
m = [1 2 3 4; 5 6 7 8; 2 3 4 5];
shift_times = {0:2, 0:3};   % 2 times for row 1, 3 times for row 2

% desird results
desired_matrices{1} = [4 1 2 3; 5 6 7 8; 2 3 4 5];
desired_matrices{2} = [3 4 1 2; 5 6 7 8; 2 3 4 5];
desired_matrices{3} = [1 2 3 4; 8 5 6 7; 2 3 4 5];
desired_matrices{4} = [4 1 2 3; 8 5 6 7; 2 3 4 5];
desired_matrices{5} = [3 4 1 2; 8 5 6 7; 2 3 4 5];

% info needed:
[rows, cols] = size(m);
count = 0;

% make all shift combinations
for shift1 = shift_times{1}
    % shift row 1
    m_shifted = m;  
    idx_shifted = [circshift([1:cols]',shift1)]';
    m_shifted(1, :) = m_shifted(1, idx_shifted);

    for shift2 = shift_times{2}
        % shift row 2
        idx_shifted = [circshift([1:cols]',shift2)]';
        m_shifted(2, :) = m_shifted(r_s, idx_shifted);       

        % store them
        store{shift1+1, shift2+1} = m_shifted;
    end
end

% store{i+1, j+1} stores row 1 shifted by i and row 2 shifted by j
% example
all(all(store{2,1} == desired_matrices{1}))  % row1: 1, row2: 0
all(all(store{2,2} == desired_matrices{4}))  % row1: 1, row2: 1
all(all(store{3,2} == desired_matrices{5}))  % row1: 2, row2: 1