Extracting elements from one matrix and placing in another

8 ビュー (過去 30 日間)
Brian DeCicco
Brian DeCicco 2021 年 6 月 16 日
コメント済み: Brian DeCicco 2021 年 6 月 16 日
Hello,
I have a matrix of size 97x97x117 (called yearly_array2). I'm trying to extract specific portions of the 3rd dimension (i.e. positions 7, 10, 34, 52, 79, 82, 88, 94, 100, & 115) and place those in a brand new matrix (called SLP_top_10). The expected size of the new matrix should be 1440x721x10. When I run the loop in my code, each iteration of "i" gets replaced by the next, and I'm left with the last postion (when i = 115) in the new matrix, when instead I want all 10 postions within my new matrix. What can I do, or how should I modify the following code, to retain all 10 positions of interest in my new array?
SLP_top_10 = zeros(97,97,10); %pre-allocating array of expected size
for i = [7 10 34 52 79 82 88 94 100 115] %positions of interest from yearly_array2
SLP_top_10 = yearly_array2(:,:,i);
end

採用された回答

Chunru
Chunru 2021 年 6 月 16 日
編集済み: Chunru 2021 年 6 月 16 日
The following will do.
i = [7 10 34 52 79 82 88 94 100 115] %posi
SLP_top_10 = yearly_array2(:,:,i);
  1 件のコメント
Brian DeCicco
Brian DeCicco 2021 年 6 月 16 日
Thank you Chunru, it works now! That was really helpful...sometimes I try to overcomplicate things by creating loops when I don't have to.

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

その他の回答 (2 件)

dpb
dpb 2021 年 6 月 16 日
MATLAB assigns the complete array to the LHS without having used subscrpting to indicate you want to place each plane extracted into a given new plane in the output array.
But, MATLAB also uses vector addressing so you "don't need no steenkin' loops!" at all.
iTop10 = [7 10 34 52 79 82 88 94 100 115];
SLP_top_10 = yearly_array2(:,:,iTop10);
  1 件のコメント
Brian DeCicco
Brian DeCicco 2021 年 6 月 16 日
Thank you! This method works as well :)

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


David Hill
David Hill 2021 年 6 月 16 日
I am confused when you said the expected size was 1440x721x10 but then your code has the corrected expected size.
SLP_top_10=yearly_array2(:,:,[7 10 34 52 79 82 88 94 100 115]);%expected size 97x97x10
  1 件のコメント
Brian DeCicco
Brian DeCicco 2021 年 6 月 16 日
Yeah sorry, I miss-typed earlier...the first 2 dimesnions should have been 97x97.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by