Info

This question is closed. Reopen it to edit or answer.

What is the best way to create an n-dimensional colon operator?

1 view (last 30 days)
Hello all,
In an attempt to vectorize some code I am working on I have run into the following issue. I am attempting to create a range of linearly spaced values with a step size of 1. What I have is two tensors (of integers) x_beg and x_end where each is size 1x1xk (note that the reason for the two singleton dimensions is that x_beg and x_end are formed by indexing into a more general mxnxk tensor). What I would like to get out is 2 tensors both of size
max_sep = max(x_end-x_beg);
max_sepx1xk
For dimension :x1xi of the first tensor I would like
nexp = x_end(1,1,i)-x_beg(1,1,i)+1;
nzeros = max_sep-nexp;
x_exp(:,1,i) = [(x_beg(1,1,i):x_end(1,1,i))';zeros(nzeros,1)];
and for the same dimension in the second tensor I would like
x_use(:,1,i) = [true(nexp,1);false(nzeros,1)];
I am trying to figure out the most efficient way to do this without using loops (since obviously that is what I am trying to avoid by using this). The reason I am looking for this kind of set up is because it is the best way I can think of in order to be able to use the results with bsxfun and logical indexing, but if someone has a better option let me know.
Just for context, I am trying to remove the loop from
for k = 1:length(imin)
for i0 = imin(1,1,k):imax(1,1,k)
for j0 = jmin(1,1,k):jmax(1,1,k)
A = X2(1,2,k).*X2(2,4,k)-X2(1,4,k).*X2(2,2,k);
B = X2(1,2,k).*X2(2,3,k)-X2(1,3,k).*X2(2,2,k)+...
(i0-X2(2,1,k)).*X2(1,4,k)-(j0-X2(1,1,k)).*X2(2,4,k);
C = (i0-X2(2,1,k)).*X2(1,3,k)-(j0-X2(1,1,k)).*X2(2,3,k);
%do some stuff with A, B, and C
end
end
end
where imin and imax, and jmin and jmax are the x_beg and the x_end values I referenced above.
Thanks, Andrew

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!