Allocating values in a 3D array using nested for loops

I've created a 3D array (A) of zeros with dimensions : A=zeros(3,2,4). Using nested for loops I need to overwrite their values so the first value on the first page is 24, and all other values reduce by one each time.
Ex:
A(:,:,1)
24 23
22 21
20 19
A(:,:,2)
18 17
16 15
14 13
Other than pre-allocating A I have no idea where to start, any help would be appreciated.

 採用された回答

James Tursa
James Tursa 2014 年 10 月 24 日

5 投票

Since this is apparently a homework assignment, I will get you started. Since the requirement is to use nested loops, I assume the instructor wants a loop for each index. So here is the outline, one loop for each index:
A=zeros(3,2,4);
for z = __:__
for y = __:__
for x = __:__
A(x,y,z) = ____;
end
end
end
I will leave it to you to fill in the blanks.

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 24 日

1 投票

A=permute(reshape(24:-1:1,2,3,4),[2 1 3])

2 件のコメント

Blake Barrows
Blake Barrows 2014 年 10 月 24 日
Thanks for your help, but I need to accomplish this using nested for loops.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 10 月 24 日
If it's a homework then try something yourself

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

カテゴリ

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

質問済み:

2014 年 10 月 24 日

回答済み:

2014 年 10 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by