Create matrix from two arrays using colon
37 views (last 30 days)
Show older comments
I'm trying to avoid using for loops in my code as much as possible. I was advised that using vectorization would be quicker and so I though it would be good practice. The problem is that I have two column vectors and I want to create a matrix that contains the values in between the two values of each column for each row. For example if the in the first row, first column, I had the value 1 and in the first row, second column I had the value 10 I want a row vector of [1,2,3,4,5,6,7,8,9,10]. I want the code to do this for all rows.
I tried this:
indexsnips(:,1) = indexstamps_start; %89x2 double
indexsnips(:,2) = indexstamps_end; %89x2 double
indexsnips = indexsnips(:,1):indexsnips(:,2); %all starts and ends have a consistent
%difference of 60. The result should be 89x6.
The result returns only the vector from the first row, but not the others.
2 Comments
Image Analyst
on 22 Feb 2014
Edited: Image Analyst
on 22 Feb 2014
Do they have to be integers? What if you have [1,10] in the first row for 10 values, and you have [1,6] in the second row for 6 values? Or do all rows have 10 values? You can't have different numbers of columns in a matrix.
What do yo umean that all rows have a consistent difference of 60? Do you mean like this
[1 61;
100, 160]
If so, how does your example with 10 agree with that? And if it's 60, and the new vector changes by 1, why do you have 6 columns instead of 60 columns?
Answers (5)
Roger Stafford
on 22 Feb 2014
Assuming 1) you want integer values in your matrix, 2) the two vectors have integers, and 3) that corresponding elements in the vectors have equal differences, then do this. Let the first column vector be u and the second one v.
M = bsxfun(@plus,v,0:v(1)-u(1));
2 Comments
Roger Stafford
on 27 Feb 2014
What you originally stated was that "I have two column vectors" but what you have used for u and v here are two row vectors. Change them to column vectors and try it again.
David Sanchez
on 27 Feb 2014
A = [100 , 200 , 300 , 400 , 500];
B = [105 , 205 , 305 , 405 , 505];
C = zeros(numel(A),numel(B)+1);
for k=1:numel(A)
C(k,:) = A(k):B(k);
end
C =
100 101 102 103 104 105
200 201 202 203 204 205
300 301 302 303 304 305
400 401 402 403 404 405
500 501 502 503 504 505
0 Comments
Jos (10584)
on 27 Feb 2014
I do not see why you have to specify the last values, since it is always the same number more than the starting values. If that is the case, you can use this
V = [100,200,300,400,500]
N = 5
% engine
C = ones(numel(V), N+1)
C(:,1) = V(:)
C = cumsum(C,2)
% or in one big unreadable but not faster step
C2 = cumsum([V(:) ones(numel(V),N)],2)
If N varies, you can use cell arrays:
V = [100,200,300,400,500]
U = [105,210,303,406, 508]
C = arrayfun(@(x) V(x):U(x),1:numel(V),'un',0)
% the cell C{k} holds the array [V(k):U(k)]
1 Comment
Jos (10584)
on 27 Feb 2014
Edited: Jos (10584)
on 27 Feb 2014
Note that you can replace N by (U(1)-V(1)) to avoid having a variable called N ...
Andrei Bobrov
on 27 Feb 2014
Edited: Andrei Bobrov
on 27 Feb 2014
v = [100,200,300,400,500];
u = [105,205,305,405,505];
M = bsxfun(@plus,min(v(:),u(:)),0:abs(v(1)-u(1)))
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!