# Converting a row to diagonal matrix

4 views (last 30 days)
MS on 18 Feb 2020
Commented: MS on 19 Feb 2020
Hello I have a row containing 120 elements i want to convert this row to 16*16 diagonal matrix with 0 in the diagonal. I tried commands like reshape and diag but still not successful. Someone please give me insight.
EDIT:
Hello Thank you guys for answers. Sorry i didnt provided much details before:
I have data from a tomography device which i need to convert it to a matrix for the software to read the data i have is in this form 1 row and 120 columns:
M(1x120)= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
But the software understand this in the following format:
Please give me some insights how can i do it.
##### 3 CommentsShow 1 older commentHide 1 older comment
Matt J on 18 Feb 2020
i want to convert this row to 16*16 diagonal matrix with 0 in the diagonal
If the result is to be both a diagonal matrix and to have zeros along the diagonal, then the matrix must simply be all zeros,
>> zeros(16)
MS on 19 Feb 2020
I have edited my question please see

Sign in to comment.

### Accepted Answer

Matt J on 18 Feb 2020
Edited: Matt J on 18 Feb 2020
Did some guessing as to what you meant, but I think this is what you want:
A=tril(true(16),-1);
B=double(A);
B(A)=rowdata;
result=B+B.',
##### 3 CommentsShow 1 older commentHide 1 older comment
Matt J on 19 Feb 2020
Isn't your question already answered? The code I already posted inserts rowdata exactly as your edited post describes.
MS on 19 Feb 2020
It worked. Thank you

Sign in to comment.

### More Answers (1)

Sky Sartorius on 18 Feb 2020
Another guess at the intended meaning of the question could be that the values should be filled along the diagonals (instead of sequentially filling in by rows or columns):
data = 1:120;
ind = tril(true(16),-1);
[M, result] = deal(zeros(size(ind)));
M(ind) = data;
M = flipud(M');
newData = M(logical(M));
result(ind') = newData;
result = result + result'
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Graphics Object Programming in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!