필터 지우기
필터 지우기

How to calculate max multiplication possibility of three adjacent numbers in 10*10 matrix?

조회 수: 5 (최근 30일)
I have a matrix of 10 to 10 size of random digits from 0 to 9. How to calculate possible max multiplication of three adjacent numbers in matrix? I would like to consider only vertical and horizontal adjacent numbers, not diagonal.
r = randi([0 9],10,10);

채택된 답변

Bruno Luong
Bruno Luong 2020년 8월 17일
% Test matrix
A=randi([0 9],10,10)
B=cat(3,movprod(A,3,1),movprod(A,3,2));
[maxB,imax]=max(B,[],'all','linear');
[i,j,k]=ind2sub(size(B),imax);
if k==1
i=max(i-1,1):min(i+1,size(A,1));
j=j+zeros(size(i));
else
j=max(j-1,1):min(j+1,size(A,2));
i=i+zeros(size(j));
end
ilin = sub2ind(size(B),i,j);
% Display the adjadcent elements
fm1 = repmat('A(%d,%d)*', 1, length(i));
fm1(end) = [];
fm2 = repmat('%d*', 1, length(i));
fm2(end) = [];
fprintf(['%d = ' fm1 ' = ' fm2, '\n'], maxB, [i(:) j(:)]', A(ilin));
  댓글 수: 3
Bruno Luong
Bruno Luong 2020년 8월 17일
The size of the array is in the first line
A=randi([0 9],10,10)
The length of the adjacent elements are in the second arguments of MOVPROD in the second statement
B=cat(3,movprod(A,3,1),movprod(A,3,2));

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 8월 17일
conv2(A, ones(1,n), 'same')
and same with ones(n, 1)
take max() over all of it.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by