MATLAB Answers

Converting a row to diagonal matrix

12 views (last 30 days)
MS
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 Comments

David Hill
David Hill on 18 Feb 2020
You description is not clear to me. Please show us what you want (example).
Matt J
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
MS on 19 Feb 2020
I have edited my question please see

Sign in to comment.

Accepted Answer

Matt J
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 Comments

MS
MS on 19 Feb 2020
Thank you for your answer
I have updated my questions with more details kindly please take a look
Matt J
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
MS on 19 Feb 2020
It worked. Thank you

Sign in to comment.

More Answers (1)

Sky Sartorius
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 Comments

Sign in to comment.


Translated by