grouping values that as need

I hava a csv file with totally random numbers all in one column, that I read from using
whoeCulomn = readtable('test2.csv');
this table have 60 values in one column,
I would like to splitt these 60 values into 10 groups in which each of these have 6 of the values. for example the frist group have from 1 ot 6 the second group have from 7 to 12 etc
How can I do that?
*the groups of my numbers should be presented such as:
x1=[the first group of six numbers]
x2=[the second group]
x3=[...];
x4=[...];
x5=[...];
x6=[...];

 Risposta accettata

An example:
x=linspace(1,60,60);
k=1;
for i =1:6:length(x)
B(k,1:6)=x(i:i+5) %; add this semicolon if you dont want this to be printed.
k=k+1;
end
B = 1×6
1 2 3 4 5 6
B = 2×6
1 2 3 4 5 6 7 8 9 10 11 12
B = 3×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
B = 4×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
B = 5×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
B = 6×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
B = 7×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
B = 8×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
B = 9×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
B = 10×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
A = 1:60;
B = reshape(A,[10,6]) %more easier way

10 Commenti

Nicle Davidson
Nicle Davidson il 5 Nov 2021
Modificato: Nicle Davidson il 5 Nov 2021
Thanks for your effort, can you explain your code? for example what have you used instead of whoeCulomn that I had put the one column from the csv file?
What are B or x representing?
Can you explain your code
Your solution does not use the csv file, the question use the attached csv file, you know
whoeCulomn = readtable('test2.csv');
% This is more easier way, X will contain 10 rows and 6 columns. You may
% rename X1=X(1,:) X2=X(2,:) etc.. X1 to X10 are stacked on top of each
% other. USE THIS
X = reshape(whoeCulomn,[10,6]);
X1=X(1,:);X2=X(2,:);
%OR THIS both are equivalent.
%The second code would be like this. This outputs of the second code and
%first code or the same.
k=1;
for i =1:6:length(whoeCulomn)
X(k,1:6)=whoeCulomn(i:i+5) %; add this semicolon if you dont want this to be printed.
k=k+1;
end
I am not sure if you got this, but for me it does not like to use reshape looks like
>> X = reshape(whoeCulomn,[10,6])
Error using tabular/reshape (line 216)
Undefined function 'reshape' for input arguments of type 'table'.
My apologies. I left the part where I convereted table to array. Please let me know if it works.
whoeCulomn = readtable('test2.csv');
whoeCulomn =table2array(whoeCulomn); % you can rename it to another variable if you want original table also.
X = reshape(whoeCulomn,[10,6]);
If you use the load command instead of readtable then, the data is loaded as a numeric matrix and we don't have to convert.
whoeCulomn=load('test2.csv');
X = reshape(whoeCulomn,[10,6]);
Nicle Davidson
Nicle Davidson il 5 Nov 2021
Modificato: Nicle Davidson il 5 Nov 2021
I see, that works on the error message, yes
If I want to work with the first line in this matrix or the fourth one or even the sixth one do you know how I can get access to that? each for it self and work with them separately
x1=[the first group of six numbers]
x2=[the second group]
x3=[...];
x4=[...];
x5=[...];
x6=[...];
How can I call these and work with them.
say I get the first six numbers in X1 or x1 and subtract x2 from this
or I get x2(1) and subtract from whole of x2 (which now is the second group of numbers in this matrix which is also the second row)
Like x2-x2(1)
you see? is it possible?
Yes it is possible.
Suppose you want to subtract X1-X2.
Then just do
Sub_Val=X(1,:)-X(2,:) % This will store X1-X2 in a new variable called Sub_val
Or you may subtract one value from all of them.
Sub_Onevalue_from_all=X(1,:)-X(2,2);% This will subtract X(2,2) from all of X(1,:)
May be this explanation is more clearer:
Instead of having has X1,X2...X10 you have X with 10 rows and 6 coloumns. Each row corresponds to each of X1,X2... So you can access them by calling the row index.
X(i,:) % will call the ith row and all the columns. So if i is 1 you are acessing X1 if it is 3
% you are acessing X3 and so on.
I appreciate your time and effort and I accept this answer. Thank you

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by