Finding diag of two cells

2 Ansichten (letzte 30 Tage)
Bahareh
Bahareh am 25 Jul. 2011
Hello,
I have two cells(a and b) each with size 1x4 and each of them contains 2x1 matrix in each of its columns. I would like to find diag([a b]) to get a 2x2 matrix but it seems that diag() is not defined for cell. Can anybody tell me what I should do?
  1 Kommentar
Nathan Greco
Nathan Greco am 25 Jul. 2011
What 2x2 matrix are you looking for? diag([a b]) would provide a 2x1 matrix. Are you looking for [diag(a) diag(b)]? When asking questions, please provide sample inputs with an example of the expected result.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Paulo Silva
Paulo Silva am 25 Jul. 2011
Maybe
cellfun(@diag,[a b],'uni',0)
  4 Kommentare
Paulo Silva
Paulo Silva am 25 Jul. 2011
Look inside each cell of the ans from cellfun ;)
c=cellfun(@diag,[a b],'uni',0)
c{1} %compare this to what you are expecting from the diag function
Bahareh
Bahareh am 25 Jul. 2011
Fantastic!!! Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Nathan Greco
Nathan Greco am 25 Jul. 2011
Does this work for you?
diag(cell2mat([a b]))
Note that the solution is the same as:
diag([a{1} a{2}])
To get the diag of both a and b, do this:
[diag(cell2mat(a)) diag(cell2mat(b))]
-Nathan
  11 Kommentare
Nathan Greco
Nathan Greco am 26 Jul. 2011
You are very unhelpful and wasted my time.
Jan
Jan am 26 Jul. 2011
@Nathan: But without doubt Bahareh is very polite and thanked your repeatedly. It was obviously not his intentention to waste your time.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by