Clear Filters
Clear Filters

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

5 views (last 30 days)
淳一
淳一 on 30 Apr 2024
Commented: Dyuman Joshi on 3 May 2024
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文などを用いて簡潔に表す方法はないでしょうか?

Answers (1)

Dyuman Joshi
Dyuman Joshi on 30 Apr 2024
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 Comments
淳一
淳一 on 30 Apr 2024
Thank you !
It has been solved.
Dyuman Joshi
Dyuman Joshi on 3 May 2024
Hello @淳一 , if my answer solved your problem, please consider accepting the answer :)

Sign in to comment.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!