Combinations of array rows with exclusion
Show older comments
I've a question that maybe has an "easy" solution. I've searched (and I still am) but haven't found yet. Can anyone could help me or point out a solution for this?
I've a result of fixed pairs (latitude longitude) for a n points / rows (usually 8 or 10 max number of points/pair of rows), now i need to combine this fixed pairs from n-1,n-2,...n-k, until n-k = 4 (minimum number of points/pair of rows). For instance, If have an array of 6x2:
[20 10;20 15;20 30;22 15;25 10; 30 10], i would need to have a first combination set of 5 rows leaving one out, then select another 5 set of rows, leaving other row out and including the first one, until every 5 set of possible row selections have been made and stored in another array with all combinations. Then, from the initail 6 row array set, I would need to select 4 rows of pairs, in the same manner i did for the selection and combination of 5 rows, until every possible 4 rows pairs of 6 have been combined.
Each row pair / point is fixed. Lat long of point 1 must always be the same lat long pair.
Maybe this is hard to do, but if anyone has some kind of solution for this it would be very helpfull
Thank you so much,
Pedro
2 Comments
Jorg Woehl
on 8 Mar 2021
Hi Pedro, if I understand you correctly you want to create all possible subarrays of your initial n-by-2 array, creating n (n-1)-by-2 arrays by leaving one row out. For example, [row1; row2; row3; row4; row5; row6] becomes [row2; row3; row4; row5; row6] or [row1; row3; row4; row5; row6] or ..., and so on.
You then said these subarrays are to be "stored in another array with all previous combinations" - how exactly? Do you want them all to be concatenated into one 30-by-2 array, one after another? Or stored in a 5-by-2-by-6 array?
Pedro Miguel Silva Pinto
on 8 Mar 2021
Edited: Pedro Miguel Silva Pinto
on 8 Mar 2021
Accepted Answer
More Answers (1)
Jorg Woehl
on 8 Mar 2021
OK, so how about this?
A = [20 10;20 15;20 30;22 15;25 10; 30 10];
cellArr{1} = deleteOneRow(A);
for i = 2:size(A,1)-4
cellArr{i} = deleteOneRow(cellArr{i-1});
end
function B = deleteOneRow(A)
rows = size(A,1);
cols = size(A,2);
matrices = size(A,3);
B = NaN([rows-1, cols, rows*matrices]);
for n=1:matrices
for row=1:rows
subA = A(:,:,n); % read one matrix from the array
subA(row,:) = []; % delete one row from it
B(:,:,(n-1)*size(A,1)+row) = subA; % ... and store it in B
end
end
end
The output is contained in the cell array cellArr. cellArr{1} gives you all possible (n-1)-by-2 matrices, cellArr{2} all possible (n-2)-by-2 matrices, and so on. This also works with A being larger than 6-by-2, such as 7-by-2, 8-by-2 etc.
4 Comments
Pedro Miguel Silva Pinto
on 8 Mar 2021
Edited: Pedro Miguel Silva Pinto
on 8 Mar 2021
Jorg Woehl
on 8 Mar 2021
Thank you, Pedro (and no, I am not with the Matlab team ;-). However, I just realized that there will be duplicates in the set of subarrays (e.g. row 1 deleted in the "first round", then row 2 in the "second round", vs. first row 2 then row 1), which you probably don't want. I have an idea for a different solution, which I'll post soon.
Pedro Miguel Silva Pinto
on 8 Mar 2021
Edited: Pedro Miguel Silva Pinto
on 8 Mar 2021
Jorg Woehl
on 8 Mar 2021
Almost done... will post later tonight (after a meeting).
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!