Indexing must appear last in an index expression

12,492

Solution 1

This error is often encountered when Y is a cell-array. For cell arrays,

Y{1}(1:3) 

is legal. Curly braces ({}) mean data extraction, so this means you are extracting the array stored in location 1 in the cell array, and then referencing the elements 1 through 3 of that array.

The notation

Y(1)(1:3)

is different in that it does not extract data, but it references the cell's location 1. This means the first part (Y(1)) returns a cell-array which, in your case, contains a single array. So you won't have direct access to the regular array as before.

It is an infamous limitation in Matlab that you cannot do indirect or double-referencing, which is in effect what you are doing here.

Hence the error.

Now, to resolve: I suspect replacing a few normal braces with curly ones will do the trick:

Y{PartNo} = CD1(1+20*(PartNo-1):20*PartNo,:);   % extract data
Z{PartNo} = Y{PartNo}(3:end)-Y{PartNo}(1:end-2);  % find the second difference
MEAN_ABS_2ND_DIFF_RESULT{PartNo} = mean(abs(Z{PartNo}));  % mean of absolute value

Solution 2

I might suggest a different approach

Y = reshape(CD1, 20, 6);
Z = diff(y(1:2:end,:));
MEAN_ABS_2ND_DIFF_RESULT = mean(abs(Z));

Solution 3

This is not a valid statement in matlab:

Y(PartNo)(3:end)

You should either make Y two-dimensional and use this indexing

Y(PartNo, 3:end)

or extract vector parts and use them directly, if you use a loop like you have shown

for PartNo = 1:6   

    % extract data                
    Y = CD1(1 + 20*(PartNo-1):20*(PartNo),:); 

    % find the second difference  
    Z = Y(3:end) - Y(1:end-2);  

    % mean of absolute value
    MEAN_ABS_2ND_DIFF_RESULT(PartNo) = mean(abs(Z));    
end

Also, since CD1 is a vector, you do not need to index the second dimension. Drop the :

Y = CD1(1 + 20*(PartNo-1):20*(PartNo));

Finally, you do not need a loop. You can reshape the CD1 vector to a two-dimensional array Y of size 20x6, in which the columns are your parts, and work directly on the resulting matrix:

Y = reshape(CD1, 20, 6);
Z = Y(3:end,:)-Y(1:end-1,:);
MEAN_ABS_2ND_DIFF_RESULT = mean(abs(Z));
Share:
12,492
Tony YEe
Author by

Tony YEe

Updated on June 06, 2022

Comments

  • Tony YEe
    Tony YEe almost 2 years

    I have a vector CD1 (120-by-1) and I separate CD1 into 6 parts. For example, the first part is extracted from row 1 to row 20 in CD1, and second part is extracted from row 21 to row 40 in CD1, etc. For each part, I need to compute the means of the absolute values of second differences of the data.

    for PartNo = 1:6   
    
        % extract data                
        Y(PartNo) = CD1(1 + 20*(PartNo-1):20*(PartNo),:); 
    
        % find the second difference  
        Z(PartNo) = Y(PartNo)(3:end) - Y(PartNo)(1:end-2);  
    
        % mean of absolute value
        MEAN_ABS_2ND_DIFF_RESULT(PartNo) = mean(abs(Z));    
    
    end
    

    However, the commands above produce the error:

    ()-indexing must appear last in an index expression for Line:2
    

    Any ideas to change the code to have it do what I want?

    • Xyand
      Xyand over 11 years
      Isn't Y(PartNo) a scalar? And the rhs a vector?
    • Xyand
      Xyand over 11 years
      So what is meaning of writing something like that: Y(1) = [1 2 3] ? And later Y(1)(3:end) - scalar indexing?
    • Rody Oldenhuis
      Rody Oldenhuis over 11 years
      Wait...I fear I've misunderstood your question. Forget my answer, go with @dustincarr's solution in stead :)
    • Rody Oldenhuis
      Rody Oldenhuis over 11 years
      Just a general remark: whitespace is FREE! There's no paper to save in a computer editor, and denser code doesn't make it any faster, so...open it up a bit, it's better on the eyes :)
    • dustincarr
      dustincarr over 11 years
      @RodyOldenhuis, you actually answered the specific question much better than I did, since the questions was specifically about the error message.
    • Rody Oldenhuis
      Rody Oldenhuis over 11 years
      @dustincarr: Ah, but it wasn't complete to the extent that the error message also occurs when doing a(2)(3), with a an ordinary matrix. But I'll un-delete my question, see what the OP thinks.
    • obchardon
      obchardon about 5 years
      Intersting to know that Octave support multiple indexing. x = 1:3 then x(1:2)(2) is supported