creating a new column with three columns

hello everyone,
i have three columns with 25 rows each. Now I want to merge all the three columns to one in a way that the first three rows will be the 1st values of all the three columns.
for ex: col_1, col_2 col_3 I need New merge col
25 22 23 25
26 25 20 22
24 28 30 23
26 and so on
so at last i will get one column with total of 75 rows
thanks in advance.

 Risposta accettata

Use this
X = reshape(X', [75, 1])

11 Commenti

thanx for the reply
but for this X should contain all 3 columns and 25 rows. but i have three different variable for three columns.
Suppose a, b, c are your columns, then define X as follows and use the expression above
X = [a b c]
Prakhar Modi
Prakhar Modi il 25 Giu 2019
Modificato: Prakhar Modi il 25 Giu 2019
the order of the rows are same. i want that the it should be arranged as the that the 1st 2nd and 3rd value of the column should the 1st value of all the three column
Don't get what you are trying to say.
for ex: col1=[1,2,3,4]
col2=[5,6,7,8]
col3=[9,10,11,12]
now i want a single column like this
output=[1,5,9,2,6,10,3,7,11,4,8,12]
Himanshu Rai
Himanshu Rai il 25 Giu 2019
Modificato: Himanshu Rai il 25 Giu 2019
This is what the code does, except all the inputs and results are (according to your question) column vectors instead of row vectors
t,,he answer it gives is,,
,output=[1,2,3,4,5,6,7,8,9,10,11,12]
where as i need this
output=[1,5,9,2,6,10,3,7,11,4,8,12]
As I said the vectors should be column vectors and not row vectors (as you mentioned in the question).
PS - Also, verify what you write properly before commenting unnecessarily.
a = [1,2,3,4]'
b = [5,6,7,8]'
c = [9,10,11,12]'
X = [a b c]
X = reshape(X', [12, 1]) % here 12 because finally there would be 12 elements
thanks. But after some operations again want to regenrate a b c from x in same order.
Use this (here X is the new modified matrix after the above operations)
X = reshape(X, [3, 25]);
X = X';
a = X(:, 1);
b = X(:, 2);
c = X(:, 3);
thanx a lot

Accedi per commentare.

Più risposte (2)

Stephan
Stephan il 25 Giu 2019
Modificato: Stephan il 25 Giu 2019
Works also if the number of lines or columns is different to 25x3:
a = [col1 col2 col3]
b = reshape(a',[],1)

3 Commenti

Prakhar Modi
Prakhar Modi il 25 Giu 2019
Modificato: Prakhar Modi il 25 Giu 2019
thanx for the reply
but for this a should contain all 3 columns and 25 rows. but i have three different variable for three columns.
see my edited answer
Prakhar Modi
Prakhar Modi il 25 Giu 2019
Modificato: Prakhar Modi il 25 Giu 2019
its just giving me the same order i want to arrange it so that the new column 1st three values should be the 1st value of old three matrix

Accedi per commentare.

Pullak Barik
Pullak Barik il 25 Giu 2019
Modificato: Pullak Barik il 25 Giu 2019
I will proceed in the following way-
1) Merge col_1, col_2, col_3 into a single matrix.
2) Reshape the transpose of the array.
The following code does the same-
res = reshape([col_1 col_2 col_3].', [], 1)

2 Commenti

its just giving me the same order i want to arrange it so that the new column 1st three values should be the 1st values of old three matrix
Are your col_1, col_2 and col_3 variables stored as column vectors or row vectors?

Accedi per commentare.

Categorie

Scopri di più su Get Started with MATLAB in Centro assistenza e File Exchange

Prodotti

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by