Error: "Index exceeds matrix dimensions." with find()

Hi, I'm trying to use the find() function to pinpoint a time when the cell culture reaches a specific cell population. However I keep getting the "Index exceeds matrix dimensions" error, and I can't figure out how to fix it. Any help would be greatly appreciated! The problem is on line 27 with the ta=t(a(:,X));
clear all; close all; clc;
%Number of cells introduced to culture
avgAreaCell = 32.5e-7; %cm^2 (range from 25 to 40 um^2)
avgAreaSkin = 20000; %cm^2
apprxTotSkinCell = 3.5e10; %Approximate amount of total skin cells on average adult
numCellBiop = input('Please enter area of biopsy in cm^2 (3cm^2 is recommended): ')/avgAreaCell; %This gives number of cells per unit area of biopsy
disp(' ')
disp(['Number of cells in biopsy is approximately: ' num2str(numCellBiop)]);
disp(' ')
%Set the number of stages & time span
N=16;
tzero=0;
tmax=35;% days/4weeks
tspan=[tzero:0.1:tmax];
I = 0; %No new cells enter. SC pop. is from biopsy
%initial conditions
Xzero=zeros(N,1);
Xzero(1)=numCellBiop;%Start Number of Cells in culture
k=2.2*ones(N-1,1); k(N)=k(1);%Transition rate constants, no death
%integrate the equations
[t,X]=ode45('cell_differentiation_equations',tspan,Xzero,[],N,I,k);
%steady state values for stages 1 to N-1
SS=I/k(1);
X_last=X(length(X),N);
a = find(X(:,N) == apprxTotSkinCell);
ta=t(a(N,:));
disp(['Time when Cell culture reaches Approx. Total of Adult Skin Cells is: ' num2str(ta) ' days']);

2 Comments

Use the debugger. Put a breakpoint on the line in question or select the 'Stop on errors' (or 'Pause on errors' from R2017b) option and then simply look at the size of the relevant components and it will be obvious what is wrong. Certainly more obvious than someone staring at the code trying to work out what size X is.
The error is on line 27 with the ta=t(a(N,:)). I ran debug mode like you suggested but I still don't see how to fix it (I'm still relatively new to Matlab)

Sign in to comment.

 Accepted Answer

First a note, never use length on a matrix. At the moment, you're in luck length(X) is the height of X but if you ever were to use a tspan with less than 16 elements, then length(X) would be the number of columns, and your line X_last=X(length(X),N); would stop working. My advice would be to never use length at all and use numel instead for vectors or size with an explicit dimension (e.g. X_last=X(size(X, 1),N); in your case) for matrices. However, here the simplest thing is to use end:
X_last = X(end, end); %or simply X_last = X(end)
As for your problem, the most likely reason for the reason is because a is empty. The reason why a would be empty would be because apprxTotSkinCell is not found in X. There's absolutely no reason why an exact value of 3.5e-12 would be returned by an ode and even if that value would be expected, due to the nature of floating point numbers you can never expect a calculation to come to an exact value.
Never compare floating point numbers with ==. Always compare their absolute difference against an arbitrarily small value suitable for your application. Hence:
tol = 1e-4; %use whatever tolerance is appropriate for your application
a = find(abs(X(:, end) - apprxTotSkinCell) <= tol);
And make sure that something is actually found:
assert(~isempty(a), 'The calculation did not return any value equal to apprxTotSkinCell');
Possibly, you want to return the index of the closest value to apprxTotSkinCell, in which case:
[~, a] = min(abs(X(:, end) - apprxTotSkinCell); %guaranteed not to be empty

1 Comment

Thank you! this worked perfectly! Also thank you for the advice. As I said, I'm still new to Matlab and I'm not clear on a lot of the little details like that.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!