Select some element of a Cell by index - faster than for structure
some small questions!
- How can I create a null N-by-N cell in matlab?
- I have a N-by-3 matrix
rv=... %# [N-by-3]
r=sqrt(sum(rv,2).^2);
if r < b
statement1
end
Statement is save the index of r in a vector (like C). Something like this: C=[1 3 4 7 9 ,…] By use of this vector I want to select some element of a N-by-N matrix Each element of a 2D matrix has 2 indexes: (i, j). If both of them be in the vector of indexes(C) then choose it and save it’s index in a cell Something like thi开发者_开发百科s: D={[2x1] [2x1] … } Thanks a lot for any guide. Whishes
=================================================================================
For the second one :
p=1
for i=1:N
if r(i)<R
L(p)=i;
p=p+1;
end
end
L=[.....];
for i=1:length(L)
for j=1:length(L)
D{i,j}=C{L(i),L(j)};
end
end
but problem is that it has a for structure and is very slow. I'am looking for something faster. perhaps with combnk
Not sure if I understood your question. But here I go.
I'd first convert indices of L from subscripts to indices with sub2ind. You can then use the resulting indices to generate a vectorize version of D, and use reshape to give it the final N-D version you want. Something like:
LInd = sub2Ind(size(C), L, L)
Dvect = C(LInd);
D = reshape(D, Shape_I_Want);
Hope this helps
精彩评论