Matlab and sorting single results to a matrix - matlab

for i=1:1:k %k = 100 people
for j=1:1:l %l = 5 orders
Resultx = a + b + d(j); % cost of drink Resultx
Resulty = f + g + c(j); % cost of food Resulty
end
Matrix(i) = [Resultsx1...j Resulty1...j]
end
Those % notes are for helping me express the problem I want to solve in my mind, and later on, in my script.
Lets claim that we want for each i to store values in a matrix of costs of the drinks and food it orders.
So for people i = 1,
1[1 5] %people 1, first order: drink costs 1 and food costs 5
2[2 3] %people 1, second order: drink costs 2 and food costs 3
...
j[x y] %people 1, j order: drink and food costs x and y
!!! Matrix(1) = sort (j [x,y]) !!!
for people i = 2,
1[1 5] %people 2, first order: drink costs 1 and food costs 5
2[2 3] %people 2, second order: drink costs 2 and food costs 3
...
j[x y] %people 2, j order: drink and food costs x and y
!!! Matrix(2) = sort (j [x,y]) !!!
for people i = k,
1[1 5] %people k, first order: drink costs 1 and food costs 5
2[2 3] %people k, second order: drink costs 2 and food costs 3
...
j[x y] %people k, j order: drink and food costs x and y
!!! Matrix(i) = sort (j [x,y]) !!!
I want to form every result of each ith iteration to a Matrix in ascending order
Matrix(i) = sort (j [x,y]).
Perhaps not the best paradigm, but thank you in advance.

(Two ways I understood your statement; I'm presuming you're interested in 2. solution. In this form Resultx and Resulty don't depend on i in any way, and therefore they'll be the same for all the "people").
1. Matrix is [ k x 2 ] array. Results from second loop are summed up!
Matrix = zeros(k, 2); % pre-allocate space
for i=1:1:k %k = 100 people
Resultx = 0;
Resulty = 0;
for j=1:1:l %l = 5 orders
Resultx = Resultx + a + b + d(j); % cost of drink Resultx
Resulty = Resulty + f + g + c(j); % cost of food Resulty
end
Matrix(i,:) = [Resultx, Resulty] % save pair
end
Sorted = sortrows(Matrix, [1 2]); % sort pairs
Last command sorts pairs, first by 1st column, then by 2nd column in ascending order. If you would like descending order for both criteria, you would use [-1 -2] instead. Combining ascending and descending is also possible (for example [-1 2]) but senselessness is questionable in this case.
2. Matrix is [ k x l x 2 ] array in which the results are kept individually and are not summed up in second loop.
Matrix = zeros(k, l, 2); % pre-allocate space
Intermediate = zeros(l, 2); % container used for sorting
for i=1:1:k %k = 100 people
for j=1:1:l %l = 5 orders
Resultx = a + b + d(j); % cost of drink Resultx
Resulty = f + g + c(j); % cost of food Resulty
Intermediate(j,:) = [Resultx, Resulty]; %save pair
end
Matrix(i,:,:) = sortrows(Intermediate, [1 2]); % sort pairs
end
Note: You should do avoid writing loops in Matlab and resort to vectorized solution wherever possible!

Related

Take a random draw of all possible pairs of indices in Matlab

Consider a Matlab matrix B which lists all possible unordered pairs (without repetitions) from [1 2 ... n]. For example, if n=4,
B=[1 2;
1 3;
1 4;
2 3;
2 4;
3 4]
Note that B has size n(n-1)/2 x 2
I want to take a random draw of m rows from B and store them in a matrix C. Continuing the example above, I could do that as
m=2;
C=B(randi([1 size(B,1)],m,1),:);
However, in my actual case, n=371293. Hence, I cannot create B and, then, run the code above to obtain C. This is because storing B would require a huge amount of memory.
Could you advise on how I could proceed to create C, without having to first store B? Comments on a different question suggest to
Draw at random m integers between 1 and n(n-1)/2.
I=randi([1 n*(n-1)/2],m,1);
Use ind2sub to obtain C.
Here, I'm struggling to implement the second step.
Thanks to the comments below, I wrote this
n=4;
m=10;
coord=NaN(m,2);
R= randi([1 n^2],m,1);
for i=1:m
[cr, cc]=ind2sub([n,n],R(i));
if cr>cc
coord(i,1)=cc;
coord(i,2)=cr;
elseif cr<cc
coord(i,1)=cr;
coord(i,2)=cc;
end
end
coord(any(isnan(coord),2),:) = []; %delete NaN rows from coord
I guess there are more efficient ways to implement the same thing.
You can use the function named myind2ind in this post to take random rows of all possible unordered pairs without generating all of them.
function [R , C] = myind2ind(ii, N)
jj = N * (N - 1) / 2 + 1 - ii;
r = (1 + sqrt(8 * jj)) / 2;
R = N -floor(r);
idx_first = (floor(r + 1) .* floor(r)) / 2;
C = idx_first-jj + R + 1;
end
I=randi([1 n*(n-1)/2],m,1);
[C1 C2] = myind2ind (I, n);
If you look at the odds, for i=1:n-1, the number of combinations where the first value is equal to i is (n-i) and the total number of cominations is n*(n-1)/2. You can use this law to generate the first column of C. The values of the second column of C can then be generated randomly as integers uniformly distributed in the range [i+1, n]. Here is a code that performs the desired tasks:
clc; clear all; close all;
% Parameters
n = 371293; m = 10;
% Generation of C
R = rand(m,1);
C = zeros(m,2);
s = 0;
t = n*(n-1)/2;
for i=1:n-1
if (i<n-1)
ind_i = R>=s/t & R<(s+n-i)/t;
else % To avoid rounding errors for n>>1, we impose (s+n-i)=t at the last iteration (R<(s+n-i)/t=1 always true)
ind_i = R>=s/t;
end
C(ind_i,1) = i;
C(ind_i,2) = randi([i+1,n],sum(ind_i),1);
s = s+n-i;
end
% Display
C
Output:
C =
84333 266452
46609 223000
176395 328914
84865 94391
104444 227034
221905 302546
227497 335959
188486 344305
164789 266497
153603 354932
Good luck!

How to determine number of hops using a vector?

I have a MATLAB matrix like below:
column no: 1 2 3 4 5 6
matrix elements 1 1 2 3 6 2
Column numbers represent node ID and elements of the matrix represent the node towards which that node points. Please help me find hop count from a particular node to node 1. I have written the following code but it doesn't solve the problem.
x = ones(1, n);
checkbit = zeros(1, n);
nodedest = [1 1 2 3 6 2];
hopcount = zeros(1, n);
for i = 1:n
for j = 1:n
if nodedest(j) == 1 && checkbit(j) == 0
hopcount(j) = hopcount(j) + 1;
checkbit(j) = 1;
else
x(j) = nodedest(j);
end
if x(j) ~= 1
hopcount(j) = hopcount(j) + 1;
x(j) = nodedest(x(j));
end
end
end
You are looking for a breadth-first search to find the shortest path in your graph. Without touching the data in any way, you can do this in O(n) time per node, given the tree-like structure of your graph:
nodedest = [1 1 2 3 6 2];
hopcount = zeros(1, 6);
for n = 2:6
k = n
while k ~= 1
hopcount(n) = hopcount(n) + 1
k = nodedest(k)
end
end
If you are willing to reverse the sense of your edges (introducing a one-to-many relationship), you could accomplish the same thing in one pass, reducing the entire algorithm from O(n2) to O(n) time complexity. The trade-off would be that memory complexity would increase from O(1) to O(n):
nodedest = [1 1 2 3 6 2];
% Reverse the input
nodesource = cell(1, 6);
nodesource(:) = {[]}
for n = 2:6
k = nodedest(n);
nodesource{k} = [nodesource{k} n];
end
% implement bfs, using the assumption that the graph is a simple tree
hopcount = zeros(1, 6);
cache = [1];
hops = 0;
while ~isempty(cache)
next = []
for c = cache
hopcount(c) = hops;
next = [next nodesource(c)]
end
hops = hops + 1;
cache = next
end

MATLAB index within groups

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?
For example, if A = [1 1 1 2 2 2 1 1 2 2] then B = [1 2 3 1 2 3 4 5 4 5].
Add 1
My own solution to this is
s = splitapply(#(x) {x, [1:numel(x)]'}, [1:numel(A)]', A(:))
B(vertcat(s{:,1})) = vertcat(s{:,2})
but it seems somewhat convoluted.
A solution using sort and accumarray:
[s is]=sort(A);
idx = accumarray(s(:),1,[],#(x){1:numel(x)});
B(is)=[idx{:}];
Another solution using the image processing toolbox:
p=regionprops(A,'PixelIdxList');
B = zeros(size(A));
for k = 1: numel(p)
B(p(k).PixelIdxList) = 1:numel(p(k).PixelIdxList);
end
Here's a solution that may not look as compact, but it's quite fast since it uses cumsum and indexing:
mA = max(A);
nA = numel(A);
ind = false(mA, nA);
ind(mA.*(0:(nA-1))+A) = true;
B = cumsum(ind, 2);
B = B(ind).';
And here are some timing results for the solutions thus far:
A = [1 1 1 2 2 2 1 1 2 2];
rahnema1: 6.51343e-05
Luis: 3.00891e-05
OmG: 2.36826e-05
gnovice: 4.93539e-06 % <---
A = randi(20, 1, 1000);
rahnema1: 0.000274138
Luis: 0.000257126
OmG: 0.000233348
gnovice: 9.95673e-05 % <---
A = randi(20, 1, 10000);
rahnema1: 0.00162955
Luis: 0.00163943
OmG: 0.00126571
gnovice: 0.00107134 % <---
My solution is the fastest for the above test cases (moderate size, moderate number of unique values). For larger cases, other solutions gain the edge. The solution from rahnema1 seems to do better as the number of unique values in A increases, whereas the basic for loop from OmG does better when the number of elements in A increases (with relatively fewer unique values):
>> A = randi(200, 1, 100000);
rahnema1: 0.0108024 % <---
Luis: 0.0931876
OmG: 0.0427542
gnovice: 0.0815516
>> A = randi(20, 1, 1000000);
rahnema1: 0.131256
Luis: 0.171415
OmG: 0.106548 % <---
gnovice: 0.124446
A solution can be using a loop to replace the values in A:
c = unique(A);
B= A;
for (idx = c)
f = find(A == idx);
B(f) = 1:length(f);
end
Here's a way that avoids loops:
s = bsxfun(#eq, A(:).', unique(A(:))); % Or, in recent versions, s = A==unique(A).';
t = cumsum(s,2);
B = reshape(t(s), size(A));

Matlab get all possible combinations less than a value

I have a matrix as follows:
id value
=============
1 0.5
2 0.5
3 0.8
4 0.3
5 0.2
From this array, I wish to find all the possible combinations that have a sum less than or equal to 1. That is,
result
======
1 2
1 4 5
2 4 5
3 5
1 5
1 4
2 4
2 5
...
In order to get the above result, my idea has been to initially compute all the possibilities of finding sum of elements in the array, like so:
for ii = 1 : length(a) % compute number of possibilities
no_of_possibilities = no_of_possibilities + nchoosek(length(a),ii);
end
Once this is done, then loop through all possible combinations.
I would like to know if there's an easier way of doing this.
data = [0.5, 0.5, 0.8, 0.3, 0.2];
required = cell(1, length(data));
subsets = cell(1, length(data));
for k = 2:length(data)-1 % removes trivial cases (all numbers or one number at a time)
% generate all possible k-pairs (if k = 3, then all possible triplets
% will be generated)
combination = nchoosek(1:length(data), k);
% for every triplet generated, this function sums the corresponding
% values and then decides whether then sum is less than equal to 1 or
% not
findRequired = #(x) sum(data(1, combination(x, :))) <= 1;
% generate a logical vector for all possible combinations like [0 1 0]
% which denotes that the 2nd combination satisfies the condition while
% the others do not
required{k} = arrayfun(findRequired, 1:size(combination, 1));
% access the corresponding combinations from the entire set
subsets{k} = combination(required{k}, :);
end
This produces the following subsets:
1 2
1 4
1 5
2 4
2 5
3 5
4 5
1 4 5
2 4 5
It is not in easy way, however is a faster way, as I removed the combination which its subsets are not passed the condition.
bitNo = length(A); % number of bits
setNo = 2 ^ bitNo - 1; % number of sets
subsets = logical(dec2bin(0:setNo, bitNo) - '0'); % all subsets
subsets = subsets(2:end,:); % all subsets minus empty set!
subsetCounter = 1;
resultCounter = 1;
result = {};
while(1)
if( subsetCounter >= size(subsets,1))
break;
end
if(sum(A(subsets(subsetCounter,:).',2)) <= 1)
result{resultCounter} = A(subsets(subsetCounter,:).',1).';
resultCounter = resultCounter + 1;
subsetCounter = subsetCounter + 1;
else
% remove all bad cases related to the current subset
subsets = subsets(sum((subsets & subsets(subsetCounter,:)) - subsets(subsetCounter,:),2) ~= 0,:);
end
end
Generate the subsets using this method. After that, check the condition for each subset. If the subset does not pass the condition, all its supersets are removed from the subsets. To do this, using sum((subsets & subsets(i,:)) - subsets(i,:),2) ~= 0 which mean get some rows from subsets which has not the same elements of the not passed subset. By doing this, we able to not to consider some bad cases anymore. Although, theoretically, this code is Θ(2^n).
Here is potential solution, using inefficient steps, but borrowing efficient code from various SO answers. Credit goes to those original peeps.
data = [0.5, 0.5, 0.8, 0.3, 0.2];
First get all combinations of indices, not necessarily using all values.
combs = bsxfun(#minus, nchoosek(1:numel(data)+numel(data)-1,numel(data)), 0:numel(data)-1);
Then get rid of repeated indices in each combination, regardless of index order
[ii, ~, vv] = find(sort(combs,2));
uniq = accumarray(ii(:), vv(:), [], #(x){unique(x.')});
Next get unique combinations, regardless of index order... NOTE: You can do this step much more efficiently by restructuring the steps, but it'll do.
B = cellfun(#mat2str,uniq,'uniformoutput',false);
[~,ia] = unique(B);
uniq=uniq(ia);
Now sum all values in data based on cell array (uniq) of index combinations
idx = cumsum(cellfun('length', uniq));
x = diff(bsxfun(#ge, [0; idx(:)], 1:max(idx)));
x = sum(bsxfun(#times, x', 1:numel(uniq)), 2); %'// Produce subscripts
y = data([uniq{:}]); % // Obtain values
sums_data = accumarray(x, y);
And finally only keep the index combinations that sum to <= 1
allCombLessThanVal = uniq(sums_data<=1)

How should I average groups of rows in a matrix to produce a new, smaller matrix?

I have a very large matrix (216 rows, 31286 cols) of doubles. For reasons specific to the data, I want to average every 9 rows to produce one new row. So, the new matrix will have 216/9=24 rows.
I am a Matlab beginner so I was wondering if this solution I came up with can be improved upon. Basically, it loops over every group, sums up the rows, and then divides the new row by 9. Here's a simplified version of what I wrote:
matrix_avg = []
for group = 1:216/9
new_row = zeros(1, 31286);
idx_low = (group - 1) * 9 + 1;
idx_high = idx_low + 9 - 1;
% Add the 9 rows to new_row
for j = idx_low:idx_high
new_row = new_row + M(j,:);
end
% Compute the mean
new_row = new_row ./ 9
matrix_avg = [matrix_avg; new_row];
end
You can reshape your big matrix from 216 x 31286 to 9 x (216/9 * 31286).
Then you can use mean, which operates on each column. Since your matrix only has 9 rows per column, this takes the 9-row average.
Then you can just reshape your matrix back.
% generate big matrix
M = rand([216 31286]);
n = 9 % want 9-row average.
% reshape
tmp = reshape(M, [n prod(size(M))/n]);
% mean column-wise (and only 9 rows per col)
tmp = mean(tmp);
% reshape back
matrix_avg = reshape(tmp, [ size(M,1)/n size(M,2) ]);
In a one-liner (but why would you?):
matrix_avg = reshape(mean(reshape(M,[n prod(size(M))/n])), [size(M,1)/n size(M,2)]);
Note - this will have problems if the number of rows in M isn't exactly divisible by 9, but so will your original code.
I measured the 4 solutions and here are the results:
reshape: Elapsed time is 0.017242 seconds.
blockproc [9 31286]: Elapsed time is 0.242044 seconds.
blockproc [9 1]: Elapsed time is 44.477094 seconds.
accumarray: Elapsed time is 103.274071 seconds.
This is the code I used:
M = rand(216,31286);
fprintf('reshape: ');
tic;
n = 9;
matrix_avg1 = reshape(mean(reshape(M,[n prod(size(M))/n])), [size(M,1)/n size(M,2)]);
toc
fprintf('blockproc [9 31286]: ');
tic;
fun = #(block_struct) mean(block_struct.data);
matrix_avg2 = blockproc(M,[9 31286],fun);
toc
fprintf('blockproc [9 1]: ');
tic;
fun = #(block_struct) mean(block_struct.data);
matrix_avg3 = blockproc(M,[9 1],fun);
toc
fprintf('accumarray: ');
tic;
[nR,nC] = size(M);
n2average = 9;
[xx,yy] = ndgrid(1:nR,1:nC);
x = ceil(xx/n2average); %# makes xx 1 1 1 1 2 2 2 2 etc
matrix_avg4 = accumarray([xx(:),yy(:)],M(:),[],#mean);
toc
Here's an alternative based on accumarray. You create an array with row and column indices into matrix_avg that tells you which element in matrix_avg a given element in M contributes to, then you use accumarray to average the elements that contribute to the same element in matrix_avg. This solution works even if the number of rows in M is not divisible by 9.
M = rand(216,31286);
[nR,nC] = size(M);
n2average = 9;
[xx,yy] = ndgrid(1:nR,1:nC);
x = ceil(xx/n2average); %# makes xx 1 1 1 1 2 2 2 2 etc
matrix_avg = accumarray([xx(:),yy(:)],M(:),[],#mean);