Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Making Matrices with Loops

1 Ansicht (letzte 30 Tage)
Gina Barlage
Gina Barlage am 26 Mai 2015
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
This is the assignment I was given:
Create a script called script19.m. The script should assume scalar value of n is assigned in the command window. The script should create an n-by-n matrix A where
A = [nn ... n2 n1;
....
2n ... 22 21;
1n ... 12 11]
An example execution is as follows:
>> n = 3;
>> script19
A =
33 32 31
23 22 21
13 12 11
This is the script I wrote but I still can't get an answer:
A = zeros(N);
for ii = N : 1
for jj = (10*ii) + N : (10* ii) + 1
A = jj
end
end
Am I on the right track??

Antworten (3)

the cyclist
the cyclist am 26 Mai 2015
You are on the right track. I can give you a couple hints:
First, be aware that the loop
for ii = N : 1
won't work. I think you want
for ii = N : -1 : 1
so that ii counts down. Similarly for your other loop.
Next, notice that inside your loop, you are not writing to specific positions in your A matrix. You'll need to fix that.

Walter Roberson
Walter Roberson am 26 Mai 2015
A = [10*(1:n)', ones(n,1)] * [ones(1,n);1:n];
  2 Kommentare
Gina Barlage
Gina Barlage am 26 Mai 2015
this needs to go from the higher number to the lower number down and across. I tried A = [10*(N: -1:1)', ones (N,1) * [ones(1.N); 1:N] and it kept giving me an error.
Thorsten
Thorsten am 26 Mai 2015
A = [10*(n:-1:1)', ones(n,1)] * [ones(1,n);n:-1:1];

Andrei Bobrov
Andrei Bobrov am 26 Mai 2015
bsxfun(@minus,((10*n+n):-10:10)',0:n-1)

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by