# ()-indexing must appear last in an index expression

1 view (last 30 days)
emar on 22 May 2017
Edited: Jan on 23 May 2017
Hello, I wrote this code
i=(find(A)~=0); % find indexes that verify this condition
% B is a cell
B(i) (end:A(i),end:A(i))=0
Matlab displays this error : ()-indexing must appear last in an index expression .
Is there any other solution to do the same operation without working with loops?
Edit
Sorry, i wasn't clear.
Here is a better version
i=(find(A)); % find indexes that verify this condition ( i is an array)
% B is a cell containing 160 matrixs
B{i} (end:-1:A(i),end:-1:A(i))=0
The purpose is to change the indexes from the index find(A) to the end to 0 ( To be simple, I wrote A as a variable, but in reality ,it's if a cropped image hasn't the same size of the windows_size)
When I did these modifications, Matlab displayed : " expected one output from a curly brace or dot indexing expression but there were 148 results"
emar on 23 May 2017
For example , if A(i) is non zero ( and A(i)= windows size - image size(i)), the borders from W(i) ( windows size) till the end of the image=0. Thus the code has to be written like
B{i} (W(i):end,W(i):end)=0
For the command end:-1:W(i), I found the code like that. It was an error.
So My question is what to do so B{i} (W(i):end,W(i):end)=0 is executed for each element of the array i ( without doing a loop on the elements of i )
Jan on 23 May 2017
Edited: Jan on 23 May 2017
The first part of this explanation is not clear already:
if A(i) is non zero
i is a vector. What does "A(i) is non zero" mean then? Which A(i)? all() or any or min or max?
Imagine A=[2, 3, 4, 0, 5]. Then i=find(A) replies i=[1,2,3,5]. A(i) is [2,3,4,5] in consequence and "A(i):end" is not useful. B{i} is a list of {B{1}, B{2}, B{3}, B{5}} and you want to set some elements to zero. But what is the meaning of setting (2:end) to 0, if (1:end) was set to 0 already?
Together with "To be simple, I wrote A as a variable, but in reality ,it's if a cropped image hasn't the same size of the windows_size" I'm completely lost. Perhaps Guillaume's answer hit your point. He is correct: There is no reason to implement this without a loop.

Guillaume on 23 May 2017
So My question is what to do ( without doing a loop on the elements of i )
You don't have a choice. There's no magic matlab syntax that can do what you want without a loop. Even arrayfun (which is arguably a loop) cannot help you in this case, since you've got an assignment.
indices = find(...)
for iidx = 1:numel(indices)
index = indices(iidx);
B{index}(W(index):end, W(index):end) = 0;
end
Until you've established that the loop is slow by actually timing it with the profile, there's no reason to avoid the loop. If it is a bottleneck, then you may have to resort to mex, or change your algorithm.