Write multiple function output in one variable

조회 수: 13 (최근 30일)
StefSter
StefSter 2022년 11월 17일
댓글: Voss 2022년 11월 18일
I'm writing a function ind = myfun(N,k) where i need the coordinates of a single index k. My input is k and the number of elements for every dimension in vector N. The Matlab routine ind2sub would work perfectly for my problem, but it writes every coordinate in an extra variable, so I would have to write [ind1, ind2, ...] = ind2sub(N,k) and then ind = [ind1, ind2, ... ]. My problem is that the number of variables is not fixed, it is an input for my function. Is it possible to write the function output of ind2sub not in several variables but in one vector?

채택된 답변

Voss
Voss 2022년 11월 17일
function ind = myfun(N,k)
[temp{1:numel(N)}] = ind2sub(N,k);
ind = [temp{:}];
end
  댓글 수: 3
StefSter
StefSter 2022년 11월 18일
Thank you very much, that worked perfectly.
Voss
Voss 2022년 11월 18일
Very good point, @the cyclist. Thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by