How to pass contents of cell array to a function?

I have
function [S , U ] = HOSVD ( A )
where U is a cell array of length 3
and I would like the function to return each cell. I tried
function [S , U{1}, U{2}, U{3} ] = HOSVD ( A )
but it doesn't work, it gives an error regarding the brackets.
How should I write it?

답변 (1개)

madhan ravi
madhan ravi 2019년 1월 4일

1 개 추천

U{:}

댓글 수: 7

Hmm, it still says "Invalid syntax at '{'. Possibly a ']' is missing"
madhan ravi
madhan ravi 2019년 1월 4일
편집: madhan ravi 2019년 1월 4일
Provide all the datas and your code. You function definition seems to be wrong. Also see how to define a function:
function [S,U{:}] = HOSVD ( A )
% U is a length 3 cell array with the
% property that U{k} is the left singular
% vector matrix of A’s mode -k unfolding
S = A ;
for mode =1:length(size(A)) %mode = 1 , 2 , 3
C = unfolding(A,mode);
[ U{mode} , Sigma , V ] = svd(C);
S = tensormat(S,U{mode}',mode);
end
for example, A is
A(:,:,1) = [ 1 2 ; 3 4 ];
A(:,:,2) = [ 10 20 ; 30 40 ];
A(:,:,3) = [ 100 200 ; 300 400 ];
and I want to call HOSVD(A) and it should return S, a tensor, and U1, U2, U3 matrices that I need later
unfolding , tensormat???
Nevermind, I just used cell2mat to convert those cell arrays and properly put them in the function def, thank you
alternatively you can use
vertcat(U{:})

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

카테고리

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

질문:

2019년 1월 4일

댓글:

2019년 1월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by