Extract 2D matrix from 3D matrix with different lengths.

I have 17x8x94059 class-double matrix. i.e 3D matrix., say "B"
Now I would like to extract 17x94059 elements and make it a 2D matix having a new variable name, say "A".
The command like
A = squeeze(B(:,1,:));
did not work.
Moreover I want to extract all 8-values from 17x8x94059 matrix in a similar way.
such that I can make a loop something like,
for i = 1:8
A = squeeze(B(:,i,:))
end
Any suggestions?

 採用された回答

Stephen23
Stephen23 2018 年 10 月 25 日
編集済み: Stephen23 2018 年 10 月 25 日

1 投票

For just one matrix use either
A = reshape(B(:,1,:),17,[]);
or
A = permute(B(:,1,:),[1,3,2]);
For all of the matrices, it is not required to use a loop:
C = num2cell(permute(B,[1,3,2]),1:2)
Using permute and num2cell is about 30% faster than using a loop (the test file is attached):
Elapsed time is 1.09111 seconds. % loop
Elapsed time is 0.718072 seconds. % num2cell

1 件のコメント

Megha
Megha 2018 年 10 月 25 日
Yes, It got my answer!
Thanks @Stephen Cobeldick

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2018 年 10 月 25 日

コメント済み:

2018 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by