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

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 日

0 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 3 月 30 日

コメント済み:

2022 年 3 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by