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

1 view (last 30 days)
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!

Accepted Answer

Simon Chan
Simon Chan on 30 Mar 2022
Edited: Simon Chan on 30 Mar 2022
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 Comments
parslee
parslee on 30 Mar 2022
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!