What is wrong with my code?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi. I am trying to write a function that takes two inputs (N and n) and return a n by n array at the top right corner of N. So far i have:
function M = top_right(N,n)
M = N(1:n, 1:n);
0 Commenti
Risposte (3)
Star Strider
il 5 Ago 2016
I’m not exactly certain what you want to do.
See if this works for you:
N = 6; % Argument
n = 3; % Argument
M = zeros(N); % Create ‘M’
M(1:n, N-n+1:N) = 1 % Set Top Right Corner To ‘1’
M =
M =
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
4 Commenti
Star Strider
il 5 Ago 2016
I don’t see any way to do what you want without specifically defining ‘N’ and ‘n’ unless you do it randomly:
N = randi(8); % Argument
n = randi(N-2); % Argument
M = zeros(N); % Create ‘M’
M(1:n, N-n+1:N) = 1 % Set Top Right Corner To ‘1’
I’m keeping the matrices small for convenience. You can also define ‘n’ as:
n = randi(N-randi(N-1));
That way, you don’t have to define anything specifically, since all the parameters are random integers.
Walter Roberson
il 5 Ago 2016
Modificato: Walter Roberson
il 5 Ago 2016
M = N(1:n, end-n+1:end);
0 Commenti
Vedere anche
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!