MATLAB: Diagonal Pattern by writing loops arraysdiagonalloopspatterns how can I print these diagonal patterns by writing a loop in a 21×21 array? Best Answer Try this: M = diag([ones(1,9) 0 ones(1,9)]);M = M + fliplr(M);M(ceil(size(M,1)/2), ceil(size(M,1)/2)) = 1;figureimagesc(M)axis('image')colormap([0 0 0; 1 1 1])No loops necessary. Experiment to get different results. Related SolutionsMATLAB: How to Assemble the matrix shown in the image on Matlab for any number of nodes n = 4; %for examplemain_diagonal = [ [-4, repmat(-3,1,n-2), -4], repmat([-5, repmat(-4,1,n-2), -5], 1, 2), [-6, repmat(-5,1,n-2), -6] ];N = length(main_diagonal);m = diag(ones(1, N-4), -4) + diag(ones(1, N-1), -1) + diag(main_diagonal) + diag(ones(1, N-1), 1) + diag(ones(1, N-4), -4) MATLAB: How do i place a 9-by-9 matrix in the center of a 256-by-256 matrix of zeros. Thank you Try this:M = zeros(255);M(123:132, 123:132) = randi(9);figure(1)imagesc(M)axis imagegridYou cannot have a (9x9) exactly in the centre of a (256x256) matrix. It is possible to put it in the centre of a (255x255) matrix. Related QuestionI have a matrix which size is mxm (square) variable and I wanna assign a mx1 matrix into the first matrix’s diagonal. What kind of loop I have to writeSpecial matrix with zeros and onesHow to Assemble this matrixHow to divide an array into sperate vectors.
Best Answer