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

7 vues (au cours des 30 derniers jours)
Mohammed Kagalwala
Mohammed Kagalwala le 29 Oct 2019
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.

Réponse acceptée

Jos (10584)
Jos (10584) le 29 Oct 2019
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)}

Plus de réponses (1)

Mohammed Kagalwala
Mohammed Kagalwala le 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)};

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by