creating a vector from a(:) : b(:) , but one for each index without using loops.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have two vectors a=[1,2,3] and b=[6,4,9]. My goal is to generate this(see below) without the use of foor loops (a single command line);
result=[a(1):b(1),a(2):b(2),a(3):b(3)];
The problem is that a and b is varying in size (but size(a)=size(b)=[1,N]).
I`ve tried
result=[a(:):b(:)];
and other variations, but it doesnt do the job. Any idea of how i can achieve this?
0 comentarios
Respuesta aceptada
Andrei Bobrov
el 1 de Nov. de 2012
Editada: Andrei Bobrov
el 1 de Nov. de 2012
a=[1,2,3];
b=[6,4,9];
result = cell2mat(arrayfun(@(x,y)x:y,a,b,'un',0));
ADD
a = randi(150,32,4,4,4); % your 4-D array
idx = {1:32,3,2,1}; % your array with indexs
[i1,i2,i3,i4] = ndgrid(idx{:});
i1 = cell(1,4);
[i1{:}] = ndgrid(idx{:});
result = a(sub2ind(size(a),i1{:}));
8 comentarios
Sean de Wolski
el 1 de Nov. de 2012
@Kjetil87, probably not, ind2sub is slow and and gets much slower for big data.
Use the for-loop just make pull out as many computations as you can which it looks like you've done already.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!