()-indexing must appear last in an index expression

4 次查看(过去 30 天)
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?
Thanks in advance
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"
  5 个评论
emar
emar 2017-5-23
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
Jan 2017-5-23
编辑:Jan 2017-5-23
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
Guillaume 2017-5-23
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by