Using 3d matrix inmatlab

A(:,:,1) =
1 1 0
0 1 1
1 1 1
A(:,:,2) =
1 0 0
0 0 1
0 0 1
I used [r,c,v] = ind2sub(size(A),find(A == 1)); to find r,c,v in matrix A that has a value 1. But I want to place a condition that while A == 1 then I(r,c) = v
for example I(1,1) = {1,2} since for v=1,2 A takes a value 1. Please help me out with a code.

 채택된 답변

Matt J
Matt J 2013년 5월 29일
편집: Matt J 2013년 5월 29일

0 개 추천

[m,n,p]=size(A);
s=reshape(1:p,1,1,p);
A=bsxfun(@times,A,s);

댓글 수: 4

jana
jana 2013년 5월 29일
I wanted to put them as sets. example I(1,1) = {1,2} that is (1,1) takes value 1 at v ={1,2}. My matrix should look more like : [{1,2} 1 1; 0 1 {1,2}; 1 1 {1,2}]
Matt J
Matt J 2013년 5월 29일
편집: Matt J 2013년 5월 29일
[m,n,p]=size(A);
s=reshape(1:p,1,1,p);
l=num2cell(bsxfun(@times,A,s),3);
l=cellfun(@(c) nonzeros(c).',l,'uni',0);
l(cellfun('isempty',l))={0}
jana
jana 2013년 5월 29일
l=cellfun(@(c) nonzeros(c).',l,'uni',0); it shows an error message : Input # 2 expected to be a cell array, was double instead.
jana
jana 2013년 5월 29일
Navermind I fixed it. Thankyou!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

질문:

2013년 5월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by