フィルターのクリア

Convert 2-D array to multidimensional array

3 ビュー (過去 30 日間)
Jim
Jim 2016 年 4 月 6 日
コメント済み: Guillaume 2016 年 4 月 6 日
I have a 2-D array with 7 columns that I want to convert to a 6-D array of dimensions 16*20*22*6*3*2. The values in the first 5 columns of the 2-D array are the first 5 indices of the 6-D array and the values in the 6th and 7th columns of the 2-D array shall be contained in the 6th dimension of the 6-D array.
I tried the following
misc(rslts1(:,1:5),1:2) = rslts1(:,6:7)
where misc is the 6-D array and rslts1 is the 2-D array but got the error "Subscripted assignment dimension mismatch"

採用された回答

Guillaume
Guillaume 2016 年 4 月 6 日
編集済み: Guillaume 2016 年 4 月 6 日
Using accumarray, you would have to duplicate your columns 1 to 5, once with 1 appended, then with 2 appended, to create the destination subscripts, and reshape the last two columns into one column:
misc = accumarray([rslts1(:, 1:5), 1; rslts1(:, 1:5), 2], reshape(rslts1(:, 6:7), [], 1));
Alternatively, you can use your syntax twice, and concatenate along the 6th dimension:
misc = cat(6, accumarray(rslts1(:, 1:5), rslts1(:, 6)), accumarray(rslts1(:, 1:5), rslts1(:, 7)));
Using sub2ind, while technically more correct, is more complicated:
misc = zeros(16, 20, 22, 6, 3, 2);
idxs = [repmat(rslts1(:, 1:5), 2, 1), repelem([1; 2], size(rslts1, 1))]; %another way to create the same matrix used in accumarray
misc(sub2ind(size(misc), idxs(:, 1), idxs(:, 2), idxs(:, 3), idxs(:, 4), idxs(:, 5), idxs(:, 6)) = rslts1(:, 6:7)
or to avoid all of these arguments in the sub2ind call (but the conversion to cell array will make the execution slower):
misc = zeros(16, 20, 22, 6, 3, 2);
idxs = num2cell([repmat(rslts1(:, 1:5), 2, 1), repelem([1; 2], size(rslts1, 1))], 1);
misc(sub2ind(size(misc), idxs{:})) = rslts1(:, 6:7)
edit: I also suggested using sparse, but that only works for 2D matrices.
  2 件のコメント
Jim
Jim 2016 年 4 月 6 日
I get an error with the first example |??? Error using ==> horzcat CAT arguments dimensions are not consistent.| and I'm trying to understand the source of it. Seems that there should be another grouping of terms in the generation of the destination subscripts.
Guillaume
Guillaume 2016 年 4 月 6 日
Do'h! Of course, trying to horzcat a column vector with a matrix is not going to work.
You can either use the construct I used in the sub2ind example:
[repmat(rslts1(:, 1:5), 2, 1); repelem([1; 2], size(rslts1, 1))];
or
[rslts1(:, 1:5), repmat(1, size(rslts1, 1), 1); rslts1(:, 1:5), repmat(2, size(rslts1, 1), 1)]
or
[[rslts1(:, 1:5); rslts1(:, 1:5)], repelem([1; 2], size(rslts1, 1))]

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

その他の回答 (2 件)

Steven Lord
Steven Lord 2016 年 4 月 6 日
Either use accumarray or sub2ind depending whether there are repeated values for the same coordinate that you want to accumulate or not.
  2 件のコメント
Jim
Jim 2016 年 4 月 6 日
I don't understand how to use sub2ind for this. I don't see how to make use of the linear index that it would provide.
Jim
Jim 2016 年 4 月 6 日
編集済み: Jim 2016 年 4 月 6 日
I understand how I can use accumarray a little better. I'm thinking of using it something like
misc = accumarray(rslts1(:,1:5), rslts1(:,6));
but I think that's not quite right. This will be assigning the values in | rslts1(:,6)| to the 5th dimension of misc instead of the 1st column of the 6th dimension.

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


Kuifeng
Kuifeng 2016 年 4 月 6 日
How about the function reshape? Take one column for example, Assume there is 126720 data. Make some changes for more columns.
a = rand([126720 1]);
a_5D = reshape(a,[16 20 22 6 3])
  1 件のコメント
Guillaume
Guillaume 2016 年 4 月 6 日
編集済み: Guillaume 2016 年 4 月 6 日
The only way that reshape would work is in the unlikely case that the indices in the first five columns happened to be ordered linearly with no gaps and no repetition. In that case, there actually would be no need for the 5 index columns.

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

カテゴリ

Help Center および 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