Subtract corresponding nonzero element from each element in a vector
1 view (last 30 days)
Show older comments
Ok, I want to create an M x M matrix A with the following.
Vec1=[0 1 1 0] Vec2=[1 3 5 7]
Then, matrix A has size M = size(Vec1) = size(Vec2) and all elements start at some constant value c.
I want to update the matrix A such that, for each index where Vec1 is not zero, create a matrix row where the row values are obtained by subtracting the value of Vec2 at that same index from each other value and getting the absolute values.
So in the above example, if I initialize matrix A as:
c c c c
c c c c
c c c c
c c c c
then, the first row stays the same (Vec 1 is zero) the second row becomes [abs(3-1), abs(3-3), abs(3-5), abs(3-7)], Third row becomes [abs(5-1), abs(5-3), abs(5-5), abs(5-7)]. Fourth row stays the same, so we end up with:
c c c c
2 0 2 4
4 2 0 2
c c c c
I would like to accomplish this without loops or possibly without even repmats and the like, since the matrices and vectors in question are huge.
Thanks!
0 Comments
Accepted Answer
Sean de Wolski
on 11 Apr 2011
%EDIT again
Vec1=[0 1 1 0];
Vec1(~Vec1) = nan;
Vec2=[1 3 5 7];
C2 = bsxfun(@times,Vec1.',abs(bsxfun(@minus,Vec2.',Vec2)));
C2(isnan(C2)) = A(isnan(C2));
7 Comments
Sean de Wolski
on 11 Apr 2011
You could remove one call to isnan by defining an index matrix:
idx = isnan(C2);
C2(idx) = A(idx);
More Answers (2)
Walter Roberson
on 11 Apr 2011
In order to avoid having at least one temporary matrix the same size as A, you would need to update A "in-place", which would require using a loop.
Possibly you might be able to do the whole calculation with a single bsxfun() call; I would have to think more about how to handle the vectorization efficiently in the face of the fact that bsxfun() expects user-provided functions to work on column vectors.
2 Comments
Sean de Wolski
on 11 Apr 2011
I wasn't able to figure out how do it all with one call to BSXFUN. I'd like to see it since it seems easily possible.
Matt Fig
on 11 Apr 2011
Try this for speed. On my machine it is much faster than the double BSXFUN call. I assume random V1 (your Vect1). .
.
.
EDIT Putting the call to ABS inside the loop is faster(?).
A = 3*ones(length(V1)); % This is your starting matrix... (Example)
TMP = 1:length(V1);
TMP = TMP(logical(V1));
for ii = TMP
A(ii,:) = abs(V2-V2(ii));
end
.
.
EDIT2
What was I thinking with that inner FOR loop? I was thinking I need to brew some more coffee...
2 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!