Select all entries in first N-1 dimensions of array

hi, i have a loop in which cell array V keeps expanding:
for it=1:10;
V{it}= ..... % the number of dimension of V{it} is 'it'
% so if it=3: size(V{it})=[2 2 2];
end
later im going to loop through V again, but i want to select only the first N dimensions:
for id=1:9;
Y{id}= ... % select first 'id' dimensions of V{id+1}
%so if id=2, W{id}=V{id+1}(:,:,1), but if id=3, W{id}=V{id+1}(:,:,:,1)
end
I just dont know how to adjust the number of ':' i want to be there. Or equivalently to adjust the numer of times to put in 1:2, ie. W{4}=V{5}(1:2,1:2,1:2,1:2,1).

3 件のコメント

Sean de Wolski
Sean de Wolski 2012 年 5 月 23 日
ARGGGHHHH!!
I just went to compliment Oleg for his answer and to up-vote it and it somehow got deleted. My apologies, Oleg and Sargondjani. Hopefully you have the Comma Separated List Expansion somewhere. Oleg, I owe you a beer.
Sargondjani
Sargondjani 2012 年 5 月 23 日
well, i dont care, i have the answer, hahaha
Oleg wrote something like:
v = [repmat({':'},id-1,1);{1}];
Y{id}=V{id+1}(v{:})
Sargondjani
Sargondjani 2012 年 5 月 23 日
o and i thanked oleg, hehe

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

 採用された回答

Daniel Shub
Daniel Shub 2012 年 5 月 23 日

0 投票

Have you thought about using linear indexing?
V{1} = rand([2,2,2]);
x = reshape(V{1}(1:4), repmat(2, 1, 2));
y = V{1}(1:2, 1:2);
isequal(x,y)
EDIT To give a full working example
Create some data:
for it=1:10;
V{it}= rand([repmat(2, 1, it), 1]);
end
Then get the data you want
for id=1:9;
Y{id} = reshape(V{id+1}(1:(2^(id))), [repmat(2, 1, id), 1]);
end

2 件のコメント

Sargondjani
Sargondjani 2012 年 5 月 23 日
the problem is that i dont see how to increase the number of "1:2" after the y=V{1}
but thanks anyway. the problem is solve with the solution that oleg gave...
Sargondjani
Sargondjani 2012 年 5 月 23 日
i see your point after the edit. thanks!! i will need this type of linear indexing later on :-)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by