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!

 Accepted Answer

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 Comments

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;

Sign in to comment.

More Answers (0)

Categories

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

Products

Release

R2021a

Tags

Community Treasure Hunt

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

Start Hunting!