Finding diag of two cells

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

Nathan Greco
Nathan Greco 2011년 7월 25일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Paulo Silva
Paulo Silva 2011년 7월 25일

0 개 추천

Maybe
cellfun(@diag,[a b],'uni',0)

댓글 수: 4

Nathan Greco
Nathan Greco 2011년 7월 25일
Note that that returns 8 2x2 matrix cells. The OP only mentioned a result of a 2x1 matrix (perhaps it was meant to be 8 of these?) Gotta love clear requests.
Bahareh
Bahareh 2011년 7월 25일
Yeah, this method gives a 2x1 cell each column with size 2x2. But how can I have 2x2 matrix? Thank you in advance.
Paulo Silva
Paulo Silva 2011년 7월 25일
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 2011년 7월 25일
Fantastic!!! Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Nathan Greco
Nathan Greco 2011년 7월 25일

0 개 추천

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

Bahareh
Bahareh 2011년 7월 25일
Thanks a lot. I need b, because a and b are different.
Nathan Greco
Nathan Greco 2011년 7월 25일
Does my answer work for you? If not, what are you looking to retrieve?
Bahareh
Bahareh 2011년 7월 25일
Your answer gives a 2x1 matrix. I need 2x2.
Nathan Greco
Nathan Greco 2011년 7월 25일
Your question asked for a 2x1 matrix. Please explain what you are looking for. Provide me example values for "a" and "b", and what you expect your result from "diag" to be. I wish I could read your mind.
Bahareh
Bahareh 2011년 7월 25일
Oh sorry, I meant 2x2 not 2x1. I edited the question. when I type a in the command window to see its value, it only gives me the size of each of its 4 columns but how can I see the value in each column?
Nathan Greco
Nathan Greco 2011년 7월 25일
One way: type a{:} and show me what it says. Another way: double click on "a" in your "Workspace" window.
Bahareh
Bahareh 2011년 7월 26일
a{1}=[ 0 0.0331]
a{2}=[0 0.0454]
a{3}=[0 0.512]
a{4}=[0 0]
I really appreciate your concern!
Nathan Greco
Nathan Greco 2011년 7월 26일
What is b supposed to be? What is the final solution to this problem supposed to be? You are leaving out important information. If I gave you a = {[1;2] [3;4] [5;6] [7;8]}; and b = {[.1;.2] [.3;.4] [.5;.6] [.7;.8]}; What would the result of your diag([a b]) be?
Bahareh
Bahareh 2011년 7월 26일
b={[0.5925 0] [0.5551 0] [0.5132 0] [0.4588 0]}. diag is not defined for cell. Anyway, thanks a lot. My problem is solved.
Nathan Greco
Nathan Greco 2011년 7월 26일
You are very unhelpful and wasted my time.
Jan
Jan 2011년 7월 26일
@Nathan: But without doubt Bahareh is very polite and thanked your repeatedly. It was obviously not his intentention to waste your time.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2011년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by