Optimise comparing multidimensional matrices

Hi. I'm trying to see where in a cell array of 21449x15, in which each cell contains a matrix of 1x7, each line of a matrix of 116255x7 occurs. Below is what I have as nested loops. It works as expected, however is obviously very slow. I'm struggling to apply any common optimisation techniques such as vectorisation or creating functions to this multidimensional problem. Would anyone have any advice please?
for i = 1:21449
% For each formula from b1 to b15
for j = 1:15
% Skip empty cases
if ~any(pep{i,j})
continue
else
% For every formula in the MFP output
for k = 1:length(formulas)
% Check if the same formula exists in the database
if isequal(formulas, pep{i,j})
% If match found, replace the formula by a score, the
% higher the number in the sequence, the higher the
% score.
result(i,j) = 1*j;
else
continue
end
end
end
end
end

댓글 수: 2

Can you upload a smaller version of your variables, say the first few hundred rows (or whatever makes sense) of pep and formulas, saved into a mat file (or two mat files)?
Also, is this line accurate as written?
if isequal(formulas, pep{i,j})
Or is it actually this?
if isequal(formulas(k,:), pep{i,j})
In any case, it would help to see (part of) the variables.
Anna
Anna 2022년 4월 23일
Thank you, that's correct, it should read formulas(k,:), I'm sorry. I have accepted the solution posted below.

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

 채택된 답변

Bruno Luong
Bruno Luong 2022년 4월 22일
편집: Bruno Luong 2022년 4월 23일

1 개 추천

I simply code by guessing what you have behind de scene. You better tell us exactly what are class/size of the variables in your pseudo code, for example the k loop doesn't oddly extract any elements from "formulas".
result = zeros(size(pep));
database = cat(1,pep{:});
tf = ismember(database, formulas, 'rows');
tf = reshape(tf, size(pep));
[~,j] = find(tf);
result(tf) = 1*j;

댓글 수: 1

Anna
Anna 2022년 4월 23일
First of all I apologise for the quality of my question, I must have accidentally deleted the (k,:), it should read formulas(k,:).
Second of all, your solution is exactly what I was after! Thank you very much. Reshaping the array didn't even occur to me, that's very clever.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2022년 4월 22일

편집:

2022년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by