Using colon operator to create evenly spaced vectors for individual elements of arrays simply and without looping
7 views (last 30 days)
Show older comments
Is there any way (other than using looping) to perform an element-by-element operation on two matrices such that a vector is created with equal spacing between the corresponding elements of each matrix? For example, if I had two matrices:
A=[1,2,3; 4,5,6] B=[12,13,14; 15,16,17]
It there a simple way to create an array (perhaps a cell or multidimensional array) that holds A(1,1):B(1,1), A(1,2):B(1,2) and so on, without manually typing all of this?
Apologies in advance if the question is unclear or the answer is obvious--I have minimal matlab experience and have run into a mental block trying to get this to work.
0 Comments
Accepted Answer
Sean de Wolski
on 1 Feb 2012
A=[1,2,3; 4,5,6];
B=[12,13,14; 15,16,17]
C = cellfun(@(x,y)x:y,num2cell(A),num2cell(B),'uni',false)
No guarantees it'll be faster than a for-loop though. The conversions using num2cell() aren't known for their speed.
2 Comments
More Answers (0)
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!