Suppose I have this array:
a = [1,2,3,4,5];
The result should be something like this:
1,2,3,4,5 2,1,3,4,5 3,1,2,4,5 4,1,2,3,5 5,1,2,3,4
How can i do this? This function must be valid for different lengths a.
a
Using a combination:
b = [a.' flipud(nchoosek(a,numel(a)-1))];
A simple solution could be:
primarySet = 1:5; result = zeros(length(primarySet)); for i = 1: length(primarySet) temp = primarySet; temp(i) = []; result(i,:) = [primarySet(i) temp]; end
Another way:
a = [10 20 30 40 50]; ind = 1:numel(a); result = a(abs(sort(bsxfun(@times, ind, 1-2*eye(numel(ind))),2)));
gives
result = 10 20 30 40 50 20 10 30 40 50 30 10 20 40 50 40 10 20 30 50 50 10 20 30 40
And one more way:
n = numel(a)-1; b = [a(:) flipud(reshape(ndgrid(a,1:n).',[],n))];
b = 1 2 3 4 5 2 1 3 4 5 3 1 2 4 5 4 1 2 3 5 5 1 2 3 4
Source: https://habr.com/ru/post/1680722/More articles:Does each server (EC2 Node) behind a load balancer need its own SSL certificate? - sslC # Packing multiple signed integers into a single 64-bit value - c #How to remove docker container even if root file system does not exist? - dockerCSS Grid vertical columns with infinite rows - cssHow do I make fun of dom elements when using mocha javascript? - javascriptSurge of the share of strollers "No space on the device" - vagrantHow to perform mocha tests in a browser? - javascriptWood of mutable pieces - rustPython, копируя структуру списка ранга 2 с разными значениями - pythonEDX - / tmp / VBoxGuestAdditions.iso: there is no space on the device (RuntimeError) - vagrantAll Articles