필터 지우기
필터 지우기

行列の要素数を変更し​、それぞれを違う行列​として表示するにはど​うすればよいですか

조회 수: 5 (최근 30일)
淳一
淳一 2024년 4월 30일
댓글: Dyuman Joshi 2024년 5월 3일
A10=ones(1,10)
A9=ones(1,9)
A8=ones(1,8)
A7=ones(1,7)
A6=ones(1,6)
A5=ones(1,5)
A4=ones(1,4)
A3=ones(1,3)
A2=ones(1,2)
A1=ones(1,1)
上記のように列の要素が1つずつ減っていく,もしくは増えていく行列を任意の数作りたいです。
for文などを用いて簡潔に表す方法はないでしょうか?

답변 (1개)

Dyuman Joshi
Dyuman Joshi 2024년 4월 30일
Preallocate a cell array, define each cell element accordingly and use indexing to access the data -
%Number of arrays
n = 10;
%Preallocating a cell array
out = cell(n,1);
for k=1:n
out{k} = ones(1,k);
end
%See the output
out
out = 10x1 cell array
{[ 1]} {[ 1 1]} {[ 1 1 1]} {[ 1 1 1 1]} {[ 1 1 1 1 1]} {[ 1 1 1 1 1 1]} {[ 1 1 1 1 1 1 1]} {[ 1 1 1 1 1 1 1 1]} {[ 1 1 1 1 1 1 1 1 1]} {[1 1 1 1 1 1 1 1 1 1]}
%Access an array via indexing e.g. 3rd array
out{3}
ans = 1x3
1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  댓글 수: 2
淳一
淳一 2024년 4월 30일
Thank you !
It has been solved.
Dyuman Joshi
Dyuman Joshi 2024년 5월 3일
Hello @淳一 , if my answer solved your problem, please consider accepting the answer :)

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

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!