Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexing must appear last in an index expression

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?

like image 225
Tony YEe Avatar asked Nov 29 '12 14:11

Tony YEe


3 Answers

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
like image 87
Rody Oldenhuis Avatar answered Oct 20 '22 00:10

Rody Oldenhuis


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));
like image 22
dustincarr Avatar answered Oct 20 '22 01:10

dustincarr


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));
like image 39
angainor Avatar answered Oct 20 '22 01:10

angainor