Referencing and extracting with conditions

1 Ansicht (letzte 30 Tage)
klb
klb am 10 Dez. 2020
Kommentiert: Image Analyst am 11 Dez. 2020
Hello everyone,
I have 2 matrices.
First, is called combomatrix of 3 columns, combinations that sum to 56 , and this is a 35640 X 3 matrix . Ther are no zeros.
Second, is letters combination of a,b and c.
parts = [a, b, a ;
b, b, c ;
c, a, b ;
b, c, b ;
a, a, b]
A maximum value associated with each letter is a = 20, b = 30, c = 35. For each row of parts matrix, I want to extract rows from thecombo matrix in the follwing manner:
For example, row 2 of parts = [b,b,c]. Referencing this vector into the combo matrix, I want to extract rows, with column 1 values are 30 or lower AND column 2 values 30 or lower AND column 3 values 35 or lower as that corresponds to the maximums for the letters associated with the vector.
How do I code for this?
Thank you for your time.

Akzeptierte Antwort

William
William am 10 Dez. 2020
I think this should work:
function y = combo(combomatrix,row)
% combomatrix is Nx3
% row is integer = row number of parts matrix to use
a = 20; b = 30; c = 35;
parts = [a, b, a ; b, b, c ; c, a, b ; b, c, b ; a, a, b];
y = combomatrix(all(combomatrix <= parts(row,:),2),:);
end
It replace the entries in the 'parts' matrix with their values. Then it select rows of the 'combomatrix' that have all values less than or equal to the selected row of parts.
  1 Kommentar
klb
klb am 11 Dez. 2020
Bearbeitet: klb am 11 Dez. 2020
Hello William
It works! I updated your code to
for run = 1: length(parts)
y = combomatrix(all(combomatrix <= parts(run,:),2),:)
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Image Analyst
Image Analyst am 10 Dez. 2020
Try this:
% Create matrix with all combinations of where 3 numbers add to 56
combomatrix = zeros(1, 3);
row = 1;
for k1 = 1 : 56
for k2 = 1 : 56
for k3 = 1 : 56
if k1+k2+k3 == 56
combomatrix(row, :) = [k1, k2, k3];
row = row + 1;
end
end
end
end
% Create part2.
a = 20;
b = 30;
c = 35;
parts = [a, b, a ;
b, b, c ;
c, a, b ;
b, c, b ;
a, a, b]
% Let's get row 2
row2 = parts(2, :)
% Get indexes where first column is less than row2(1).
col1Indexes = combomatrix(:, 1) <= row2(1);
% Get indexes where second column is less than row2(2).
col2Indexes = combomatrix(:, 2) <= row2(2);
% Get indexes where third column is less than row2(3).
col3Indexes = combomatrix(:, 3) <= row2(3);
% Find out where all three conditions are true.
goodRows = col1Indexes & col2Indexes & col3Indexes;
% Extract those rows from combomatrix
goodMatrix = combomatrix(goodRows, :)
You get good matrix, a matrix of 695 rows.
  2 Kommentare
klb
klb am 11 Dez. 2020
Bearbeitet: Image Analyst am 11 Dez. 2020
Thank you IA ! It works.
Stepwise commenting is much appreciated. I updated to this:
for run = 1: length(parts)
myrow = parts(run, :)
goodRows = combomatrix(:, 1) <= myrow(1) & combomatrix(:, 2) <= myrow(2) & combomatrix(:, 3) <= myrow(3);
% Extract those rows from combomatrix
goodMatrix = combomatrix(goodRows, :)
end
For now, I accepted another answer and keeping this for future use.
I think Mathworks should start allowing 'Accepting' of more than one answer.
Image Analyst
Image Analyst am 11 Dez. 2020
Right now, no answer is Accepted. Did you unaccept it? Please "Accept" your favorite answer. You can also "Vote" for the answer you accepted, plus Vote for any additional other answers to give all people who helped you "reputation points".

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 10 Dez. 2020
Bearbeitet: Matt J am 10 Dez. 2020
N=size(parts,1);
result=cell(N,1);
for i=1:N
rows=all(combomatrix<=parts(i,:),2);
result{i}=combomatrix(rows,:);
end
  1 Kommentar
klb
klb am 11 Dez. 2020
Hello Matt,
It returns a summary table telling me how many rows meet the criterion for each combination of parts matrix. However, it does not return the matrix comprising of the rows themselves that meet the criteron. I updated to this and it worked; which I am seeing now is same as accepted answer. Thank you for your time though!
N=size(parts,1);
for i=1:N
rows=combomatrix((all(result2<=parts(i,:),2)),:)
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by