v = [2 1 4];
D = diag(v)
Having a diagonal matrix, i wanted to split the diagonal matrix as below
M1 = [
2 0 0
0 0 0
0 0 0]
M2 = [
0 0 0
0 1 0
0 0 0]
M3 = [
0 0 0
0 0 0
0 0 4]
working for any dimension diagonal matrix

댓글 수: 1

Jos (10584)
Jos (10584) 2019년 3월 18일
What do you mean by "working for any dimension diagonal matrix" ?
Any number of elements in V? In that case, see my answer below.

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

 채택된 답변

KSSV
KSSV 2019년 3월 18일

1 개 추천

v = [2 1 4];
nx = length(v) ;
M = zeros(nx,nx,nx) ;
for i = 1:nx
M(i,i,i) = v(i) ;
end

추가 답변 (1개)

Jos (10584)
Jos (10584) 2019년 3월 18일

1 개 추천

Having a vector V with any number of elements, this does the job:
V = [2 1 4]
n = numel(V) ;
M = zeros(n,n,n) ;
M(linspace(1, n^3, n)) = V
If you have a diagonal matrix D, use V = diag(D) first.

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

질문:

2019년 3월 18일

댓글:

2019년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by