problem with repeating index values
1 次查看(过去 30 天)
显示 更早的评论
Hi,
Suppose a = [0 0 0 0]; if I do:
a([1 1 2 2 2])= a([1 1 2 2 2])+ 1;
I get: [1 1 0 0];
However, what I really want is:
[2 3 0 0];
What would be the easiest way do achieve this?
0 个评论
回答(5 个)
Tom
2012-8-15
I'm not 100% sure what you're trying to do, but is this what you're after?
a = [0 0 0 0];
Index=[1 1 2 2 2];
for n=1:numel(Index)
a(Index(n))=a(Index(n))+1;
end
2 个评论
Matt Fig
2012-8-15
Pinpress comments,
Thanks -- yes that's what I was trying to do. However, it has a potentially very slow "for" loop for a large dataset, which isn't what I am looking for.
Image Analyst
2012-8-16
Wow. How many elements do you have? I ran through 10 million iterations of Tom's for loop in a fraction of a second. So having a for loop doesn't seem to add much overhead. Do you have more than, say, a billion iterations to do?
Tom
2012-8-15
编辑:Tom
2012-8-15
This avoids using a loop, but it might be more limited than what you require. I extended the indexing array so you can a bit more about what happens (the bsxfun finds all values in the index that equal each unique value of the index, and this is summed to get the amount each one occurs)
a = [0 0 0 0];
Index=[2 2 3 3 3];
U=unique(Index);
a(U)=sum(bsxfun(@eq,Index,U'),2)
(Apologies if every time you read this I've edited it)
0 个评论
Star Strider
2012-8-15
编辑:Star Strider
2012-8-17
This is my solution:
a = [0 0 0 0];
idx = [1 1 2 2 2]'
A = accumarray(idx, ones(size(idx))) EDIT -> A = accumarray(idx, 1)
uidx = unique(idx)
a(uidx) = A'
Likely not as efficient a solution as you would like, but it sort of gets you there.
2 个评论
Walter Roberson
2012-8-16
You can use A = accumarray(idx, 1) instead of constructing the ones() array.
Caution: if the unique indices were 1 and 3, then A would have indices 1, 2, 3, with the 2 position being a zero. You would not assign a([1 3]) to be the three values from A, because that would be a length mis-match. But you could use
a(uidx) = A(uidx);
The reason for doing that rather than a = A'; is that there might be trailing positions in "a" that were not touched by the indices.
Mind you, another way of dealing with that situation would be:
a = [0 0 0 0];
idx = [1 1 2 2 2]'
a = accumarray(idx, 1, [max(idx) 1]) .';
Star Strider
2012-8-16
编辑:Star Strider
2012-8-16
I discovered later, after I remembered that accumarray demands a column vector rather than the initial row vector (that it did not like) for the first argument, that it doesn't have any strong opinions about the second. I initially misinterpreted its error message and thought it wanted vectors of the same length for both arguments. (The ones vector needs to be replaced by ‘1’ for esthetics if not efficiency, but I didn't edit my code because then your comment wouldn't have a context.)
As I understand the question and the preferred outcome, there are trailing positions in a that aren't touched by the indices. That's the reason I wrote it as I did, i.e.:
However, what I really want is:
[2 3 0 0];
Maybe I missed something.
Anyway, with Bruno Luong not here, I wanted to be sure accumuarray got a mention!
Andrei Bobrov
2012-8-16
a = [0 0 0 0];
Index=[1 1 2 2 2];
s = regionprops(Index,'Area');
a(unique(Index)) = [s.Area];
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!