How do I multiply the numbers in a 2D array using a for loop, must be a for loop as part of the course requirements

2 ビュー (過去 30 日間)
Hi everyone, this is very basic stuff but I'm new to this. All the steps so far are required by the course.
B=[1:24]
B=reshape(B,6,4)'
A1=B(:,4)
A2=B([2 4],[1 4])
B([3 4],[5 6])=[0 1;1 0]
The next step it to 'create a matrix A3 (using a for/end loop) that has coefficients which are the square of the sine of the corresponding values in matrix B (in degrees), eg. if B(2,3)=20, then A3(2,3)=0.1170',
Now, I'm ok with the sin^2 bit, I managed to get an array with but not using a for loop.
>> sin(B*pi/180).^2
ans =
0.0003 0.0012 0.0027 0.0049 0.0076 0.0109
0.0149 0.0194 0.0245 0.0302 0.0364 0.0432
0.0506 0.0585 0.0670 0.0760 0 0.0003
0.1060 0.1170 0.1284 0.1403 0.0003 0
We need to know how to use the for loop for this for later stage work.
Any help would be greatly appreciated, thank you

採用された回答

Matt J
Matt J 2023 年 1 月 29 日
編集済み: Matt J 2023 年 1 月 29 日
One way:
A3=nan(size(B));
for i=1:size(B,2)
A3(:,i)=sind(B(:,i)).^2;
end
  3 件のコメント
Matt J
Matt J 2023 年 1 月 30 日
編集済み: Matt J 2023 年 1 月 30 日
size(B,2) gives the number of columns B(:,i).
B=rand(3,5)
B = 3×5
0.3184 0.4519 0.5605 0.1580 0.3940 0.8615 0.8785 0.7961 0.3876 0.0718 0.1113 0.6686 0.2878 0.3678 0.8770
size(B,2)
ans = 5

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

その他の回答 (0 件)

カテゴリ

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