Why do I keep getting this error with my for loop?

1 次查看(过去 30 天)
I'm writing a for loop and I keep getting an error that says, "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 25-by-2."
I'm not sure what I'm doing wrong so help would be very much appreciated!
I have a matrix of 25x4 that goes from 1 to 100 in increment of 1.
I would like the odd columns to be multiplied using the exponential function equation y = ae^(-bx) and even columns using y = ae^(bx).
And the following is my attempt to at the for loop
ky_e = linspace(1,100,100);
ky_e = reshape(ky_e,25,[]);
ky_e1 = ky_e(:,1:2:end);
ky_e2 = ky_e(:,2:2:end);
y_e1 = zeros(25,2);
y_e2 = zeros(25,2);
coeffs(1) = 0.8655;
coeffs(2) = -0.0049;
for i = 1:2:4
for n = 1:25
y_e1(n,i) = coeffs(1)*exp(coeffs(2)*-ky_e1);
end
end
for i = 2:2:4
for n = 1:25
y_e2(n,i) = coeffs(1)*exp(coeffs(2)*ky_e2);
end
end
Also is there a way to combine y_e1 and y_e2 so that y_e = [y_e1(:,1) y_e2(:,1) y_e1(:,2) y_e2(:,2)]?
Because the size of the data I will be using to run this code is much larger than the example shown here!

采纳的回答

Simon Chan
Simon Chan 2022-3-30
编辑:Simon Chan 2022-3-30
Remove the for loops, just use
y_e1=coeffs(1)*exp(coeffs(2)*-ky_e1);
y_e2= coeffs(1)*exp(coeffs(2)*ky_e2);
y_e = [y_e1(:,1), y_e2(:,1), y_e1(:,2), y_e2(:,2)];
  2 个评论
parslee
parslee 2022-3-30
y_e = [y_e1(:,1), y_e2(:,1), y_e1(:,2), y_e2(:,2)]
This would be the way I would do it if I only had a few columns for y_e1 and y_e2, but the actual data that I need to use has 50 columns for both, so doing it this way would not be efficient.

请先登录,再进行评论。

更多回答(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