MATLAB Answers

0

concatenate column values in a vector

Asked by Elysi Cochin on 20 Nov 2019 at 18:05
Latest activity Answered by Steven Lord
on 20 Nov 2019 at 18:18
i have a vector
v1 = [ 1,0,1,0] (dim 1x4 double)
i wanted to join the values in v1 to a new variable
v2 = [1010] (dim 1x1 double)
how to do so

  0 Comments

Sign in to comment.

3 Answers

Answer by KALYAN ACHARJYA on 20 Nov 2019 at 18:09
Edited by KALYAN ACHARJYA on 20 Nov 2019 at 18:12
 Accepted Answer

Simpler way:
v=[1,0,1,0];
result=str2num(sprintf('%1d',v))
here the same question Credit: @per isakson
Result:
result =
1010
>>

  0 Comments

Sign in to comment.


Answer by Erivelton Gualter on 20 Nov 2019 at 18:09

You can use the following line of code:
v2 = sum(v1*diag(10.^(length(v1)-1:-1:0)));

  0 Comments

Sign in to comment.


Answer by Steven Lord
on 20 Nov 2019 at 18:18

Treat v1 as the coefficients of a polynomial and evaluate that polynomial for x = 10.
v1 = [ 1,0,1,0];
v2 = polyval(v1, 10)

  0 Comments

Sign in to comment.