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=[...];
採用された回答
Sudharsana Iyengar
2021 年 11 月 5 日
編集済み: Sudharsana Iyengar
2021 年 11 月 5 日
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 件のコメント
Nicle Davidson
2021 年 11 月 5 日
編集済み: Nicle Davidson
2021 年 11 月 5 日
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
Sudharsana Iyengar
2021 年 11 月 5 日
編集済み: Sudharsana Iyengar
2021 年 11 月 5 日
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
Nicle Davidson
2021 年 11 月 5 日
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'.
Sudharsana Iyengar
2021 年 11 月 5 日
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]);
Sudharsana Iyengar
2021 年 11 月 5 日
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
2021 年 11 月 5 日
編集済み: Nicle Davidson
2021 年 11 月 5 日
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?
Sudharsana Iyengar
2021 年 11 月 5 日
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
Sudharsana Iyengar
2021 年 11 月 5 日
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,:)
Sudharsana Iyengar
2021 年 11 月 5 日
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.
Nicle Davidson
2021 年 11 月 5 日
I appreciate your time and effort and I accept this answer. Thank you
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
タグ
参考
2021 年 11 月 5 日
2021 年 11 月 5 日
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
