Azzera filtri
Azzera filtri

How do i do this

1 visualizzazione (ultimi 30 giorni)
Hunter Herrenkohl
Hunter Herrenkohl il 18 Giu 2018
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 Commenti
John D'Errico
John D'Errico il 18 Giu 2018
Define "agree". Is that a synonym for "is the same as" or "isequal to"?
Hunter Herrenkohl
Hunter Herrenkohl il 18 Giu 2018
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

Accedi per commentare.

Risposta accettata

Ankita Bansal
Ankita Bansal il 18 Giu 2018
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 Commento
Hunter Herrenkohl
Hunter Herrenkohl il 18 Giu 2018
Thank you!! This works awesome

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by