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 views (last 30 days)
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

Accepted Answer

Matt J
Matt J on 29 Jan 2023
Edited: Matt J on 29 Jan 2023
One way:
A3=nan(size(B));
for i=1:size(B,2)
A3(:,i)=sind(B(:,i)).^2;
end
  3 Comments
Matt J
Matt J on 30 Jan 2023
Edited: Matt J on 30 Jan 2023
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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!