필터 지우기
필터 지우기

how to i attain a matrix with 1 to n

조회 수: 6 (최근 30일)
Austin
Austin 2013년 10월 1일
답변: Austin 2013년 10월 1일
when i prompt a user for a value for m,n how do i attain a matrix such that the first row of a matrix be 1 to m for eg.if a user input a value of 9 for m the first row of a matrix will be: 1 2 3 4 5 6 7 8 9
similarly, if the user enter a value of 6 for n, then my first column will be 1 2 3 4 5 6
please kindly advise,thanks alot!
  댓글 수: 2
Wayne King
Wayne King 2013년 10월 1일
You have not said how you are filling in the other entries of the matrix.
We can't have the user get 9 columns with your row vector and then have one column of 6 elements. You have created a 9x6 matrix, how do you want us to fill in the missing elements?
Austin
Austin 2013년 10월 1일
hi,Wayne King,as for the other missing elements will be zeros.

댓글을 달려면 로그인하십시오.

채택된 답변

Wayne King
Wayne King 2013년 10월 1일
편집: Wayne King 2013년 10월 1일
See my comment above. If you want us to fill the remaining elements with zeros.
dims = input('Provide the row and column dimension of the matrix in brackets, e.g. [6 9] \n');
% the user enters [6 9]
X = zeros(dims(1),dims(2));
X(:,1) = 1:dims(1);
X(1,:) = 1:dims(2);

추가 답변 (3개)

Austin
Austin 2013년 10월 1일
r=input('please inducate the number of rows you want for the matrix') c=input('please indicate the number of columns you want for the matrix') mat=zeros(r,c)
this is what i have so far..

Austin
Austin 2013년 10월 1일
편집: Wayne King 2013년 10월 1일
r=input('please indicate the number of rows you want for the matrix')
c=input('please indicate the number of columns you want for the matrix')
mat=zeros(r,c)
mat(:,c)=1:r
mat(r,:)=1:c
but i keep getting this error..:
Subscripted assignment dimension mismatch.
Error in Untitled7 (line 6) mat(:,c)=1:c
  댓글 수: 1
Wayne King
Wayne King 2013년 10월 1일
You changed your code so now it works, initally you had the wrong numbers in your column and row assignment.
The above will work but you are just filling the last column and last row, is that what you want?

댓글을 달려면 로그인하십시오.


Austin
Austin 2013년 10월 1일
arrr i found out that i entered r and c in the wrong line,thanks alot Wayne King your answer did helped me alot!

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by