필터 지우기
필터 지우기

How do i do this

조회 수: 1 (최근 30일)
Hunter Herrenkohl
Hunter Herrenkohl 2018년 6월 18일
댓글: Hunter Herrenkohl 2018년 6월 18일
I have
R = input('Please enter a number of rows for the matrix')
C = input('Please enter a number of columns for the matrix')
M = randi(2, R, C)
for M2 = 1:numel(M)
if M(M2) > 1
M(M2) = -1
end
end
M3 = M
for MN = 1:numel(M3)
I need to: Part c: You will need to use a loop to check the neighbors of each element and then count how many agree with the current element you are on. You will need to check the elements to the left, right, above and below. You will probably need to create another matrix to keep track of the similar spin count.
How do i do this?
  댓글 수: 2
John D'Errico
John D'Errico 2018년 6월 18일
Define "agree". Is that a synonym for "is the same as" or "isequal to"?
Hunter Herrenkohl
Hunter Herrenkohl 2018년 6월 18일
equal to, i have a RxC matrix (the dimensions indicated from the user) filled with 1s and -1s. I have to find out how many are equal to their neighbors and how many of their neighbors they are equal to in order to figure out if they will change or not

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

채택된 답변

Ankita Bansal
Ankita Bansal 2018년 6월 18일
Hi, I am assuming that you are trying to compare M3(i,j) with it's adjacent elements and you want to store the number of times M3(i,j) matches with adjacent elements in a new variable c(i,j). You can do this by creating a new matrix of size (R+2,C+2).
R = input('Please enter a number of rows for the matrix');
C = input('Please enter a number of columns for the matrix');
M = randi(2, R, C);
M(M>1)=-1; % instead of "for M2 = 1:numel(M) if M(M2) > 1 M(M2) = -1 end end"
M3 = M;
M4=NaN(R+2,C+2);
M4(2:end-1,2:end-1)=M3;
c=zeros(R,C);
for i = 2:R+1
for j=2:C+1
arr = [M4(i-1,j) M4(i+1,j) M4(i,j+1) M4(i,j-1)];
c(i-1,j-1)=nnz(ismember(arr,M4(i,j)));
end
end
  댓글 수: 1
Hunter Herrenkohl
Hunter Herrenkohl 2018년 6월 18일
Thank you!! This works awesome

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by