Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

1 view (last 30 days)
clc
clear
L_s=180;
L_m=710;
L_h=740;
L_e=3850;
L_o=4720;
Q_sh=67;
Q_mh=36;
Q_he=161;
Q_eo=182;
Q_oo=212;
M=linspace(0, 710*6, 6);
[N(1,:)]=ClConc(180, M(1), 740, 3850, 4720); %problematic line according to Matlab
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
[N(2,:)]=ClConc(180, M(1), 740, 3850, 4720);
[N(3,:)]=ClConc(180, M(1), 740, 3850, 4720);
[N(4,:)]=ClConc(180, M(1), 740, 3850, 4720);
[N(5,:)]=ClConc(180, M(1), 740, 3850, 4720);
hold on
plot(M, N(:,1))
plot(M, N(:,2))
plot(M, N(:,3))
plot(M, N(:,4))
plot(M, N(:,5))
xlabel('loads of chlorine')
ylabel('concentration of chloride(ppm)')
title('loads of chloride vs concentration of chloride(ppm)')
legend('s', 'm', 'h', 'e', 'o')
hold off
function [c]= ClConc(L_s, L_m, L_h, L_e, L_o)
L=[L_s; L_m; L_h; L_e; L_o];
Q=[67 0 0 0 0; 0 36 0 0 0; -67 -36 161 0 0; 0 0 -161 182 0; 0 0 0 -182 212];
c=(Q./L);
end
  1 Comment
Dyuman Joshi
Dyuman Joshi on 2 Nov 2022
1 - The output from your function is a matrix (5x5). You can not assign a matrix to a row.
2 - M is 6x1 where as N(:,i) will be 5x1. There will be a mismatch in plotting.
M=linspace(0, 710*6, 6)
M = 1×6
0 852 1704 2556 3408 4260
N=ClConc(180, M(1), 740, 3850, 4720)
N = 5×5
0.3722 0 0 0 0 NaN Inf NaN NaN NaN -0.0905 -0.0486 0.2176 0 0 0 0 -0.0418 0.0473 0 0 0 0 -0.0386 0.0449
function [c]= ClConc(L_s, L_m, L_h, L_e, L_o)
L=[L_s; L_m; L_h; L_e; L_o];
Q=[67 0 0 0 0; 0 36 0 0 0; -67 -36 161 0 0; 0 0 -161 182 0; 0 0 0 -182 212];
c=(Q./L);
end

Sign in to comment.

Answers (2)

Voss
Voss on 2 Nov 2022
clc
clear
L_s=180;
L_m=710;
L_h=740;
L_e=3850;
L_o=4720;
Q_sh=67;
Q_mh=36;
Q_he=161;
Q_eo=182;
Q_oo=212;
M=linspace(0, 710*6, 6);
result = ClConc(180, M(1), 740, 3850, 4720)
result = 5×5
0.3722 0 0 0 0 NaN Inf NaN NaN NaN -0.0905 -0.0486 0.2176 0 0 0 0 -0.0418 0.0473 0 0 0 0 -0.0386 0.0449
function [c]= ClConc(L_s, L_m, L_h, L_e, L_o)
L=[L_s; L_m; L_h; L_e; L_o];
Q=[67 0 0 0 0; 0 36 0 0 0; -67 -36 161 0 0; 0 0 -161 182 0; 0 0 0 -182 212];
c=(Q./L);
end
The result returned from ClConc is a 5x5 matrix. You try to assign it into a 1-by-something vector. That cannot work, and MATLAB lets you know that via an error message.

Cris LaPierre
Cris LaPierre on 2 Nov 2022
The result of your function ClConc is a 5x5 matrix. The error is because you are trying to assign it to a single row of N. You have to assign it to something of the same size (5x5) or reshape it so that it is a single row.
Should the output of ClConc be 5x5?
If so, you may need to do something like this
N(1,:)=reshape(ClConc(180, M(1), 740, 3850, 4720),1,[]);

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by