Effacer les filtres
Effacer les filtres

Replace all numbers within a matrix

1 vue (au cours des 30 derniers jours)
Krish Desai
Krish Desai le 27 Sep 2015
I'm trying to change all the numbers in a matrix between 1: n^2
My code is:
function output = IsMagic(n)
A= zeros(n);
r=1;
c=1;
row_index = 0;
while row_index <= r
col_index=0;
while col_index <= c
A(r,c) = randi(1*n^2);
col_index=col_index + 1;
end
row_index= row_index +1;
end
output=A;
end
  1 commentaire
Stephen23
Stephen23 le 27 Sep 2015
Modifié(e) : Stephen23 le 27 Sep 2015
I formatted your code for you, but in future please do it yourself by selecting the code text and clicking the {} Code button that you will find above the text box.
Please do not put blank lines between every line of your code.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 27 Sep 2015
I’m not quite certain I understand what you want to do. This will produce a (n x n) matrix with your random integers:
A= zeros(n);
r=n;
c=n;
row_index = 1;
while row_index <= r
col_index=1;
while col_index <= c
A(row_index,col_index) = randi(1*n^2);
col_index=col_index + 1;
end
row_index= row_index +1;
end
One problem is that you set ‘r’ and ‘c’ both to 1, so you were only ever addressing A(1,1). I changed those to n, and the row and column references for ‘A’ to ‘row_index’ and ‘col_index’. I also started those at 1 instead of 0. The code now produces the matrix I believe you want to create. Change the code as necessary so it does what you want.

Plus de réponses (0)

Catégories

En savoir plus sur Logical 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