Converting a row to diagonal matrix

3 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2020-2-18
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 2020-2-19
I have edited my question please see

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2020-2-18
编辑:Matt J 2020-2-18
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 个评论
Matt J
Matt J 2020-2-19
Isn't your question already answered? The code I already posted inserts rowdata exactly as your edited post describes.

请先登录,再进行评论。

更多回答(1 个)

Sky Sartorius
Sky Sartorius 2020-2-18
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'

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by