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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by