Why do I keep getting this error with my for loop?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
parslee
am 30 Mär. 2022
Kommentiert: Simon Chan
am 30 Mär. 2022
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!
0 Kommentare
Akzeptierte Antwort
Simon Chan
am 30 Mär. 2022
Bearbeitet: Simon Chan
am 30 Mär. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!