MATLAB Answers

0

How to set the diagonal of a cell array of matrices?

Hi,
I'm looking to set the diagonal of my cell array to identity, specifically eye(3,3). I know a simple for loop solution exists, but if there's something more elegant please let me know.
Thank you.

  0 Comments

Sign in to comment.

2 Answers

Answer by Jos (10584)
on 29 Oct 2019
 Accepted Answer

Well found, Mohammed! You should put as an answer here :-)
Here is another method
A = cell(4,4) % a cell array
n = size(A,1)
A(1:(n+1):end) = {eye(3,3)}

  1 Comment

Sign in to comment.


Answer by Mohammed Kagalwala on 29 Oct 2019

I found the solution ! For a cell array A of size n x n, one can do the following
A(logical(eye(n,n))) = {eye(3,3)};

  0 Comments

Sign in to comment.