Why do I get the "index exceeds matrix dimensions error"?

Hello, I really need some help with the code below. I am trying to take a random row from EVLP and add the values to sequential rows in DLP. I thought I had it but I keep getting the error index exceeds matrix dimensions. I hope that makes sense and someone can help.

EVLP = table2array(EVLoadprofilestop10);
DLP = table2array(Domesticloadprofiles);
    for i = 1:length(EVLP)
        ind = randi(size(EVLP,1));
        row = EVLP(ind,:);
        DLP = DLP(i,:) + row;
        i+1;
    end

1 Comment

i+1;
calculates i+1 and then discards the value. It is not doing anything useful in your code, but it is also not hurting other than the confusion it is causing.
Note: do not do i=i+1; at that point. Your for loop is taking care of incrementing i.

Sign in to comment.

 Accepted Answer

length() of an array is max(size(TheArray)) which is not necessarily the first dimension and is not necessarily the second dimension.

Your ind and row are okay, as you are correctly only using up to the maximum row number.

We are not given any information about size of DLP, and even if the number of rows matched, the length() you use could be causing problems.

But the real problem is that DLP = DLP(i,:) + row is extracting one particular row of DLP on the right side, adding to it, and then storing the resulting row as the complete DLP array. You should be using

DLP(i,:) = DLP(i,:) + row;

3 Comments

Hi Walter,
DLP is a 50x336 double and EVLP is a 10x336 double. So I am taking a row from EVLP and trying to sum the elements with a row from DLP.
What if I want to have the rows that I haven't got to yet the same and just overwrite the rows as I get to them?
Then your for i should be
for i = 1 : size(DLP,1)
I do not understand your question about overwriting.
By the way, you can do all of this without a loop:
ind = randi(size(EVLP,1), size(DLP,1), 1);
DLP = DLP + EVLP(ind, :);
Thank you! It is now working as I wanted it to. Regarding overwriting I misunderstood what you meant and therefore was asking something that you had already answered.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!