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

1 次查看(过去 30 天)
淳一
淳一 2024-4-30
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!