How to convert multiple tables into matrices
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello Folks,
I'm trying to write a function, which converts every inputs as table in matrices. I used to use myTable{:,:} to do this for single input. Now it doesn't work anymore.
採用された回答
Ameer Hamza
2020 年 6 月 17 日
For dealing with multiple tables, it is best to create a cell array
T = {myTable1, myTable2, myTable3};
Then you can function such as cellfun to create a cell array of matrices
M = cellfun(@(x) {x{:,:}}, T); % implicit for-loop
or create a single large matrix
M = [M{:}]; % connect horizontally
M = vertcat(M{:}); % connect vertically
8 件のコメント
Viet-Anh Do
2020 年 6 月 17 日
Hi, many thanks for the quick answer. The number of tables is not given. It's like they give you a bunch of table. Use a single function to convert all of them into matrices and and then use matrix operation to solve the problem. I did the latter one and need a solution for the matrices
I'm newbie as well ...
Ameer Hamza
2020 年 6 月 17 日
Can you show an example of how the input tables are given, and what is your intended output?
must be like this: (table of n x n)
function [] = score(rawTable1,rawTable2)
% convert table into matrix
rawMatrix = rawTable1{:,:};
rawMatrix = rawTable2{:,:};
% then alot of matrix operations
end
now when I call the function, it should be like this:
score(table1,table2,table3,table 4...)
the function should convert them all in matrices and the output of these are a mean matrix, that includes the mean of every cells from each matrix
Viet-Anh Do
2020 年 6 月 17 日
編集済み: Viet-Anh Do
2020 年 6 月 17 日
each time I run the function, the numbers of tables are different (is what I mean with ... in call function)
if the description does not clarify my problem, I can eventually post my code here if you have time for this. Thanks in advance
You can define a function like this
function [] = score(varargin)
rawMatrix = cellfun(@(x) {x{:,:}}, varargin);
% rawMatrix{1} is matrix for table1
% rawMatrix{2} is matrix for table2
% ..
% rawMatrix{end} is matrix for the last table
end
It accepts arbitrarily many tables. For example, call it like this
myTable1 = array2table(rand(5));
myTable2 = array2table(rand(5));
myTable3 = array2table(rand(5));
T = score(myTable1)
T = score(myTable1, myTable2)
T = score(myTable1, myTable2, myTable3)
All these calls are valid.
rawMatrix is a cell array. Following code shows how to take the mean of all matrices if all of them are of same dimensions
function y = score(varargin)
N = nargin; % number of tables
rawMatrix = cellfun(@(x) {x{:,:}}, varargin);
% rawMatrix{1} is matrix for table1
% rawMatrix{2} is matrix for table2
% ..
% rawMatrix{end} is matrix for the last table
y = mean(cat(3, rawMatrix{:}), 3);
end
Viet-Anh Do
2020 年 6 月 17 日
Thank you. It works! You save my ass today.
Ameer Hamza
2020 年 6 月 18 日
I am glad to be of help! :)
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Tables についてさらに検索
参考
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)
