How to make a matrix with the entries being the number of even indices of that entry?

3 vues (au cours des 30 derniers jours)
I need to make a 12x12 matrix with the entries being the number of even indices of the entry.
So 1,1 would be 0, 1,2 would be 1 and 2,2 would equal 2.
The task is specifically using for loops or the meshgrid function.
Any help would be appreciated.
  3 commentaires
Joshua Balfour
Joshua Balfour le 16 Sep 2019
Modifié(e) : Joshua Balfour le 16 Sep 2019
mat = zeros(12,12);
x = 1:12;
y = 1:12;
[X, Y] = meshgrid(x,y);
a = mod(X,2);
b = mod(Y,2);
matrix = a + b;
This gives a matrix of:
matrix =
2 1 2 1 2 1 2 1 2 1 2 1
1 0 1 0 1 0 1 0 1 0 1 0
2 1 2 1 2 1 2 1 2 1 2 1
1 0 1 0 1 0 1 0 1 0 1 0
2 1 2 1 2 1 2 1 2 1 2 1
1 0 1 0 1 0 1 0 1 0 1 0
2 1 2 1 2 1 2 1 2 1 2 1
1 0 1 0 1 0 1 0 1 0 1 0
2 1 2 1 2 1 2 1 2 1 2 1
1 0 1 0 1 0 1 0 1 0 1 0
2 1 2 1 2 1 2 1 2 1 2 1
1 0 1 0 1 0 1 0 1 0 1 0
but this gives the odd indices, I want the even ones.

Connectez-vous pour commenter.

Réponses (1)

Jos (10584)
Jos (10584) le 13 Sep 2019
help meshgrid
help rem

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by