Filter löschen
Filter löschen

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

5 Ansichten (letzte 30 Tage)
淳一
淳一 am 30 Apr. 2024
Kommentiert: Dyuman Joshi am 3 Mai 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文などを用いて簡潔に表す方法はないでしょうか?

Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!