Create matrix from two arrays using colon

조회 수: 12 (최근 30일)
Daniel
Daniel 2014년 2월 22일
댓글: Walter Roberson 2017년 11월 17일
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
Image Analyst
Image Analyst 2014년 2월 22일
편집: Image Analyst 2014년 2월 22일
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?
Daniel
Daniel 2014년 2월 27일
What I have is something like this:
blah(:,1) = [100 , 200 , 300 , 400 , 500];
blah(:,2) = [105 , 205 , 305 , 405 , 505];
I tried
m = blah(:,1):blah(:,2);
what I got was only the first row
m =
100 101 102 103 104 105
what I want is a matrix like this
m =
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

댓글을 달려면 로그인하십시오.

답변 (5개)

Roger Stafford
Roger Stafford 2014년 2월 22일
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
Daniel
Daniel 2014년 2월 27일
편집: Daniel 2014년 2월 27일
I tried a test run on some arbitrary code:
v = [100,200,300,400,500];
u = [105,205,305,405,505];
M = bsxfun(@plus,v,0:v(1)-u(1))
answer was:
M =
Empty matrix: 5-by-0
what I want is:
m =
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
Roger Stafford
Roger Stafford 2014년 2월 27일
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
David Sanchez 2014년 2월 27일
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

Jos (10584)
Jos (10584) 2014년 2월 27일
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
Jos (10584)
Jos (10584) 2014년 2월 27일
편집: Jos (10584) 2014년 2월 27일
Note that you can replace N by (U(1)-V(1)) to avoid having a variable called N ...

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2014년 2월 27일
편집: Andrei Bobrov 2014년 2월 27일
v = [100,200,300,400,500];
u = [105,205,305,405,505];
M = bsxfun(@plus,min(v(:),u(:)),0:abs(v(1)-u(1)))

Perry Liu
Perry Liu 2017년 11월 17일
편집: Perry Liu 2017년 11월 17일
indexsnips(:,1)+(0:60)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by