MATLAB: How to generate the matrix of (50,100) with its diagonal alone having values matrix generation how to generate the matrix of (50,100) with its diagonal alone having values Best Answer Method one: indexing:M = zeros(50,100);M(1:51:50^2) = V % vector V must have 50 elementsA simple example:>> M = zeros(5,10);>> M(1:6:25) = 1:5M = 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0Method two: diag:[diag(V),zeros(50)] % vector V has 50 elementsand an example:>> [diag(1:5),zeros(5)]ans = 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 Related SolutionsMATLAB: Generate a diagonal matrix from the elements of another matrix I would recommend using diag function.The following is an example:A = [1 2 3 4 5; 6 7 8 9 10];A = A';output = diag(A(:));>> outputoutput = 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 10 MATLAB: How to replace and join 2 matrix with one of them is random generated matrix A dull FOR loop approach as proof of concept:for i2 = 1:size(data2, 1) index = data2(i2, 1); if data1(index, 2) == 0 data1(index, 7) = data2(i2, 2); endendDoes this match your needs already? Then the solution would be much shorter than the problem description. Related QuestionUse the diag command to find matrix and find dominant eigenvaluesMatrix manipulation from one to another oneWhat’s the solutionHow to create a matrix with the diagonals set to 10 and non-diagonals a random integer (0 to 2) where each row sums to 10 (without including diagonal)
Best Answer