필터 지우기
필터 지우기

Referencing and extracting with conditions

조회 수: 2 (최근 30일)
klb
klb 2020년 12월 10일
댓글: Image Analyst 2020년 12월 11일
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.

채택된 답변

William
William 2020년 12월 10일
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
klb
klb 2020년 12월 11일
편집: klb 2020년 12월 11일
Hello William
It works! I updated your code to
for run = 1: length(parts)
y = combomatrix(all(combomatrix <= parts(run,:),2),:)
end

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2020년 12월 10일
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
klb
klb 2020년 12월 11일
편집: Image Analyst 2020년 12월 11일
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 2020년 12월 11일
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".

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2020년 12월 10일
편집: Matt J 2020년 12월 10일
N=size(parts,1);
result=cell(N,1);
for i=1:N
rows=all(combomatrix<=parts(i,:),2);
result{i}=combomatrix(rows,:);
end
  댓글 수: 1
klb
klb 2020년 12월 11일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by