Matrix creation issue concerning indexing

Summary:
I need this function to take the data and build a matrix row by row.
Inputs:
m = 1
n = 2000
o = 2001
p = 4000
data = 11x1 structure containing 4000x1 arrays in each cell of data points
int = [0 3 0 0 3 0 0 3 0 0]
"int" codes designate whether stimulation is seen in the first or second interval period
Function:
function [patch] = matrix_buidler (m,n,o,p,data,int);
for i = 1:length(int);
if (int(i)==0)
a = data(i).num(:,1);
stim(i,:) = a(m:n);
% display(stim(1:10,1));
else
a = data(i).num(:,1);
stim(i,:) = a(o:p);
% display(stim(1:10,1));
end
patch=stim;
end
end
Error:
??? Index exceeds matrix dimensions.
Error in ==> matrix_builder at 5
stim(i,:) = a(m:n);
What I don't understand:
The length of int is the number of data files, so how is it possible for the index to exceed and since I'm building the matrix here, why is that even an issue?

Réponses (1)

You are getting that problem because the "n" you are passing to the routine exceeds the number of rows in some data(i).num(:,1) . One of the ways that could happen is if one of the data(i).num is a row vector instead of a column vector. You could generalize your routine by coding
a = data(i).num;
as that would handle both row vectors and column vectors. And of course you could put in verification tests such as
if n > numel(a)
fprintf(2, 'not as much data as expected at data(%d).num, has %d elements but requested subset was %d:%d\n', i, numel(a), m, n);
end

5 commentaires

Zeke Merchant
Zeke Merchant le 25 Mai 2015
Didn't seem to work. What I don't understand is that I have checked each data file and they're all 4000 excel cells long. I've generalized the routine as you suggested (great idea, thank you!) but it's still giving me the same error.
Walter Roberson
Walter Roberson le 25 Mai 2015
When the error shows up, what are size(stim) and size(a) and what are m and n?
I notice by the way that your int is length 10 but your data is length 11. I would be concerned about a potential length mismatch. The line before the one shown would be the line that would have problems if your int was longer than your data, and just maybe it is showing the wrong line (but it shouldn't.)
Zeke Merchant
Zeke Merchant le 25 Mai 2015
My apologies. Int should be [0 3 3 0 0 3 0 0 3 0 0].
The size of "a" is [a,b]=[1,1]. Does that mean that a is being stored as a cell instead of an array? It should be 4000x1. The error occurs before I can calculate the size of stim.
What does class(a) show? And it might be worth doing
arrayfun(@(K) class(data(K).dim), 1:length(data), 'Uniform', 0)
to check in case values are not consistent.
Zeke Merchant
Zeke Merchant le 25 Mai 2015
class(a) produces "double." So it's not a cell array then, right? If it was then it would call it a cell or structure or something.
The arrayfun line wouldn't work. Says that "dim" is undefined.
So the reason that the index exceeds dimension is because it's not even pulling the data from the structure correctly in the first place. Right?

Cette question est clôturée.

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by