MATLAB: How can I merge matrices

for loopmatrixvector

Z is a vector and X is a matrix
for h = 1:length(Z)
[D , E] = find(Z(h) == X)
end
vector D and E overwrite in every loop. how can i conserve them in a vector?

Best Answer

Even better would be to avoid the loop entirely by using bsxfun:
>> F = [1,2,3]
F =
1 2 3
>> B = [0,1;3,2;0,Inf;9,8]
B =
0 1
3 2
0 Inf
9 8
>> out = bsxfun(@eq,B,reshape(F,1,1,[]));
>> idx = find(out)
idx =
5
14
18
And of course you can use ind2sub to get more specific index information:
>> [row,col,pag] = ind2sub(size(out),idx)
row =
1
2
2
col =
2
2
1
pag =
1
2
3