0
votes

I would like to scan a matrix all rows. Take all first columns or x columns and put them into another new matrix. How can i do that ?

example matrix below

    input_matrix_training={
{0.645569620253164,0.443037974683544,0.177215189873418,0.0253164556962025};
{0.620253164556962,0.379746835443038,0.177215189873418,0.0253164556962025};
{0.594936708860759,0.405063291139241,0.164556962025316,0.0253164556962025};
}

For example lets scan rows and make new matrix with only first column

result : 0.645569620253164, 0.620253164556962, 0.594936708860759

thank you

matlab,matrix

2

2 Answers

2
votes

First of all, what you have is a cell, which makes it much more difficult. If you can, enter the data like this:

input_matrix_training=[
  0.645569620253164 0.443037974683544 0.177215189873418 0.0253164556962025
  0.620253164556962 0.379746835443038 0.177215189873418 0.0253164556962025
  0.594936708860759 0.405063291139241 0.164556962025316 0.0253164556962025];

Once you have that, it's quite easy.

input_matrix_training(:,1)

Or the first row by:

input_matrix_training(1,:)
1
votes

Use the colon operator. In the example below, (:,1) means all rows, column 1.

Take first column:

first_column = input_matrix_training(:,1);

Take columns 2 to 4:

two_to_four = input_matrix_training(:,2:4);

Take first x columns:

x = 3;
first_x = input_matrix_training(:,1:x);