Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Indexing error

Subject: Indexing error

From: Oluwa KuIse

Date: 12 Jul, 2010 15:35:21

Message: 1 of 5

Hello,
I am receiving this error message for the LAST line of this code. "??? In an assignment A(I) = B, the number of elements in B and I must be the same."

%Start of program
clear all
close all
clc
InfilDepth = 0.001*ones(10,12); %Cumulative Infiltration Depth
AntecedentMoisture = 0.4*ones(10,12);
InitialAntecedentMoisture = 0.3*ones(10,12);
dIdt = zeros(10,12);
del_t =1;
Lambda = 0.23;
SatMoisture = 0.6*ones(10,12);
ResidMoisture = 0.1*ones(10,12);
AirEntry = 0.4;
HydCond = 1e-7;
% Update the Cumulative Infiltration Depth
InfilDepth = InfilDepth + dIdt*del_t; % from definition of dIdt
wf =(AirEntry./((AntecedentMoisture - ResidMoisture)./(SatMoisture - ResidMoisture)).^(1/Lambda)); %Wetting Front suction head
dIdt(AntecedentMoisture >= 0.3*SatMoisture + 0.7*InitialAntecedentMoisture) = HydCond;
%This is the line MATLAB has a problem with:
dIdt(AntecedentMoisture < 0.3*SatMoisture + 0.7*InitialAntecedentMoisture) = HydCond./(1-exp(-InfilDepth./((SatMoisture - AntecedentMoisture).*wf)));

What am I doing wrong?
Thanks!

Subject: Indexing error

From: Garrett

Date: 12 Jul, 2010 16:12:19

Message: 2 of 5

> dIdt(AntecedentMoisture < 0.3*SatMoisture + 0.7*InitialAntecedentMoisture) = HydCond./(1-exp(-InfilDepth./((SatMoisture - AntecedentMoisture).*wf)));


My guess is that your matrices that you are using the ./ operator on are not the same size. Check to make sure that '-InfilDepth' and '((SatMoisture - AntecedentMoisture).*wf))' are matrices of the same size.

Subject: Indexing error

From: Oluwa KuIse

Date: 12 Jul, 2010 16:33:18

Message: 3 of 5

They are of the same size. If fact, the whole RHS is of the same size as the expression in () on the LHS which is what makes the error message, well, interesting.
Thanks for the suggestion.

Subject: Indexing error

From: Garrett

Date: 12 Jul, 2010 17:11:04

Message: 4 of 5


> dIdt(AntecedentMoisture < 0.3*SatMoisture + 0.7*InitialAntecedentMoisture) = HydCond./(1-exp(-InfilDepth./((SatMoisture - AntecedentMoisture).*wf)));

Looked at it harder and I think I know your problem.

The number of values in I or 'AntecedentMoisture < 0.3*SatMoisture + 0.7*InitialAntecedentMoisture' is variable depending on the logical condition.

The number of values in B is set to a 10x12 matrix. So unless every single value in dldt meets the logical condition then you should run into that error.

-Garrett

Subject: Indexing error

From: Steven Lord

Date: 12 Jul, 2010 17:19:28

Message: 5 of 5


"Oluwa KuIse" <wespeakforex@yahoo.com> wrote in message
news:i1fg4e$g1r$1@fred.mathworks.com...
> They are of the same size. If fact, the whole RHS is of the same size as
> the expression in () on the LHS which is what makes the error message,
> well, interesting.
> Thanks for the suggestion.

The error message is _slightly_ misleading. Since you're performing logical
indexing on that line, the number of elements on the right-hand side must be
equal to the number of _nonzeros_ in the logical indexing expression on the
left-hand side (or the right-hand side must be a scalar, using scalar
expansion, or the explicit empty [] to delete elements.) I'll note that to
the developers.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us