필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
parslee
parslee 2022년 3월 30일
댓글: Simon Chan 2022년 3월 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.
Simon Chan
Simon Chan 2022년 3월 30일
y_e = reshape([y_e1;y_e2],[],2*size(y_e1,2))

댓글을 달려면 로그인하십시오.

추가 답변 (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