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!

 采纳的回答

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 个评论

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by