Azzera filtri
Azzera filtri

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

5 visualizzazioni (ultimi 30 giorni)
淳一
淳一 il 30 Apr 2024
Commentato: Dyuman Joshi il 3 Mag 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文などを用いて簡潔に表す方法はないでしょうか?

Risposte (1)

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

Accedi per commentare.

Categorie

Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange

Tag

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!