フィルターのクリア

How can I split a table at given rows?

29 ビュー (過去 30 日間)
funkadelala
funkadelala 2015 年 2 月 23 日
コメント済み: Sad Grad Student 2015 年 2 月 23 日
I have an n by m table, A, and a vector of indices, idx.
Is there a way to split my table into smaller tables, each beginning with the row indicated in idx?
For example:
A = ['str1', 'str2';
1, 2;
3, 4;
'str3', 'str4';
5, 6]
idx = [1; 4]
I would like:
A1 = ['str1', 'str2';
1, 2;
3, 4]
A2 = 'str3', 'str4';
5, 6]
Many thanks.

回答 (3 件)

Sad Grad Student
Sad Grad Student 2015 年 2 月 23 日
Try:
A1 = A(idx(1):idx(2)-1,:);
A2 = A(idx(2):end,:);
you can make that into a loop too if you have more values.

funkadelala
funkadelala 2015 年 2 月 23 日
Thank you. For looping, I have:
B = zeros(1,length(idx));
for i = 1:1:length(idx)
B(i) = A(idx(i):idx(i+1)-1,:);
end
But, this returns the error:
"The following error occurred converting from table to double: Error using double. Conversion to double from table is not possible."
I assume that this is because of mixed data types. How might I correct this?
  3 件のコメント
funkadelala
funkadelala 2015 年 2 月 23 日
編集済み: funkadelala 2015 年 2 月 23 日
Curly indexing did the trick. The working code is:
B = cell(length(idx),1); %preallocates dimensions
for i = 1:1:length(idx)
%creates cell array that contains tables; each cell beginning with
%index idx(i) of table A, ending on index idx(i+1)-1
if i ~= length(idx) %if not last pass
B{i,:} = A(idx(i):idx(i+1)-1,:);
else
B{i,:} = A(idx(i):end,:);
end
end
Thanks!
Sad Grad Student
Sad Grad Student 2015 年 2 月 23 日
Cool! Congrats :)

サインインしてコメントする。


Sad Grad Student
Sad Grad Student 2015 年 2 月 23 日
Solution to this question:
B = cell(length(idx),1); %preallocates dimensions
for i = 1:1:length(idx)
%creates cell array that contains tables; each cell beginning with
%index idx(i) of table A, ending on index idx(i+1)-1
if i ~= length(idx) %if not last pass
B{i,:} = A(idx(i):idx(i+1)-1,:);
else
B{i,:} = A(idx(i):end,:);
end
end
  1 件のコメント
Sad Grad Student
Sad Grad Student 2015 年 2 月 23 日
Had to write it that way so someone having the same question can access the actual working solution! :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by