ind2sub output dimension

1 回表示 (過去 30 日間)
Mohsen  Davarynejad
Mohsen Davarynejad 2011 年 12 月 10 日
My question is regarding the output of the ind2sub function. In my code, since the input dimention of ind2sub changes, i should expect the output dimention to change. But a code like this is not working.
B = rand(6,6,6);
A(1:ndims(B)) = ind2sub(size(B),56);
The output of the above code is [56 56 56] while I'm expecting something like [2 4 2].
Any suggestion?

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 12 月 11 日
Follow the same clue in the answer to your other question, you can do the following:
B=rand(4,5,6);
Ind=(20:25).';
n=ndims(B);
Sub=cell(1,n);
[Sub{:}]=ind2sub(size(B),Ind)
SubMatrix=cell2mat(Sub)
Sub =
[6x1 double] [6x1 double] [6x1 double]
SubMatrix =
4 5 1
1 1 2
2 1 2
3 1 2
4 1 2
1 2 2
  1 件のコメント
Mohsen  Davarynejad
Mohsen Davarynejad 2011 年 12 月 11 日
Thanks.

サインインしてコメントする。

その他の回答 (1 件)

bym
bym 2011 年 12 月 10 日
you need multiple assignments for that function.
[r,c,p]=ind2sub(size(B),56)
r =
2
c =
4
p =
2
  2 件のコメント
Paulo Silva
Paulo Silva 2011 年 12 月 10 日
that one was bugging me for some time, +1 vote
Mohsen  Davarynejad
Mohsen Davarynejad 2011 年 12 月 10 日
But as I've mentioned, the output dimention is changing. So I do not know if the I need three variables, in your case r, c, p, of if I need more.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDirected Graphs についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by