Making a NxN matrix.

I have a homework assignment and i'm trying to figure out some functions that could help me write the matrix in this form (on that link i posted) like if i wanted to make a row with 1000 ones and not have to write 1 a thousand times.. In my class they don't teach us the software so yeah, any help will be appreciated, thank you.

回答(2 个)

We don't make homeworks, we only help with answers to specific questions and if we are kind enough we might give some tips:
doc ones %execute this to see the documentation of the function ones
doc zeros %execute this to see the documentation of the function zeros
Simple example:
m=zeros(10,10); %create one array with 10 rows and columns full of zeros
m(5,:)=1; %all columns (:) from row number 5 are now equal to 1
m %see the array
Like I said in my response to your post of this in the newsgroup:
Alan: To set an entire row to a value:
M(rowNumberToSet, :) = theValue;
Example:
M(42, :) = 2.718281828;
To set an entire column so a value:
M(:, columnNumberToSet) = theValue;
Example:
M(:, 69) = 3.14159;
This is basic MATLAB stuff - the stuff you learn the first couple of hours into it. You should find it early on in the "Getting Started" section of the help.

5 个评论

if i want a pattern like this one:
{1 1 1 1}
{1 2 2 2}
{1 2 3 3}
{1 2 3 4}
where pretty much what this is is a nxn where n is 4. what i noticed was
A(1,:)=1
A(:,1)=1
A(2,2:)=2
A(3,3:)=3
A(4,4:)=4
does this mean: A(n, n:)=n ???
im trying to develop a while loop to get this pattern.. i know C programming i'm currently in that class as well but as i said before we dont get Matlab trayning in Linear Algebra so im not familiar with all the commands.. how do i make a for loop or while loop on a case like this one.. i know how to do it for other operations, just not in a matrix form. I'd appreciate some intuition in this.
training***
Creates your matrix with loop.
% 1.
n = 4;
for j = n:-1:1, A(j,j:n)=j; A(j:n,j)=j; end
% 2
A=ones(n);
for j = 1:n, A(j:n,j:n)=j; end
Hi Alan! Creates your matrix without loops.
% 1
A = repmat(1:4,4,1);
A = min(A,A.');
% 2
A = repmat(1:4,4,1);
A = tril(A) + triu(A.',1)
Yaser Rostami comments to Andrei Bobrov
clear all; close all; clc m=zeros(5,7); g=0; c=numel(m); for d=1:c; g=1+g; m(d)=g; end

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by