フィルターのクリア

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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by