Create array based on a loop setting a variable

Hi, I hope I am able to explain my situation well enough to receive help. I am just starting with Matlab and basically a complete newbie.
I imported a table with two columns and 104 rows from an Excel sheet. It contains measured data of the variables I and U, so 104 values for I and 104 values for U.
To work with this table (called "A), I created two arrays:
I=A(1:104,1);
U=A(1:104,2);
So I and U are two arrays with the size of 104x1.
Now I need to multiply each row of I with each row of U and ideally get another array with the size of 104x1. To multiply each row individually, I created a small loop
for i=1:104
P=I(i)*U(i);
end
This works, but to my understanding, it just sets the variable P to the last result of the multiplication (of row 104).
I need to save every result of each multiplication to an array, but I can't find a function to do this. Any help is appreciated, thank you in advance!

 채택된 답변

Walter Roberson
Walter Roberson 2022년 3월 18일

1 개 추천

If you want corresponding rows, then you can simply use
P = I .* U;
with no loops needed.
This syntax multiplies "corresponding" positions in the arrays.

댓글 수: 2

Peter
Peter 2022년 3월 18일
Well, that's easier than the loop, thank you very much!
Now my only issue is to set the results to the correct formatting (in the axes of the plot, too). Currently, the results are in the scale of 5*10^4, but I'd like it shown as 50*10^3.
x = 1 : 10;
y = randi(65536, size(x));
ax = gca;
plot(ax, x, y);
ax.YAxis.Exponent = 3;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2022년 3월 18일

댓글:

2022년 3월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by