1つのデータにXY要​素を持たせて行列を作​成する方法

30 views (last 30 days)
Naoya Oka
Naoya Oka on 18 Dec 2022
Commented: Naoya Oka on 18 Dec 2022
MATLAB初心者です。
「0」「1」で判別したデータを下図のように並び替えをしたいです。
(X座標をExcelのA~Z...などに、Y座標データをExcelの1~nに反映させたい)
現在、X座標とY座標、それらに対応した「0」「1」の各種csv列データがあります。
そこでMATLAB内で行列をつくり、Excelにインポートしたいのですが
「0」「1」のデータにXY座標のデータを持たせ、行列を作る方法が分かりません。
有識者の方の力を貸していただきたいです。

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 18 Dec 2022
Edited: Atsushi Ueno on 18 Dec 2022
質問の添付図をExcelファイル(zero_one.xls)に起こしました。これをmapとします。
まず逆順でmapを元のデータ(org)に変換します。それをまた、orgからmapに変換します。
readmatrix, writematrix, meshgrid, sortrows, reshape の各関数を使用しました。
%% Excelデータ(map)から逆順で元のデータを再現する
map = readmatrix('zero_one.xls');
[idx,idy] = meshgrid(1:size(map,1),1:size(map,2)); % X座標とY座標を作る
org = [idx(:),idy(:),map(:)]; % 元データ
writematrix(org,'index.csv'); % X座標とY座標、それらに対応した「0」「1」の各種csv列データを再現
type index.csv
1,1,0 1,2,0 1,3,0 1,4,0 1,5,0 1,6,0 1,7,0 1,8,0 1,9,0 1,10,0 1,11,0 1,12,0 1,13,0 1,14,0 1,15,0 1,16,0 1,17,0 1,18,0 1,19,0 1,20,0 1,21,0 1,22,0 1,23,0 1,24,0 1,25,0 1,26,0 1,27,0 1,28,0 1,29,0 1,30,0 1,31,0 1,32,0 1,33,0 1,34,0 1,35,0 1,36,0 1,37,0 1,38,0 1,39,0 2,1,0 2,2,0 2,3,0 2,4,0 2,5,0 2,6,0 2,7,0 2,8,0 2,9,0 2,10,0 2,11,0 2,12,0 2,13,0 2,14,0 2,15,0 2,16,0 2,17,0 2,18,0 2,19,0 2,20,0 2,21,0 2,22,0 2,23,0 2,24,0 2,25,0 2,26,0 2,27,0 2,28,0 2,29,0 2,30,0 2,31,0 2,32,0 2,33,0 2,34,0 2,35,0 2,36,0 2,37,0 2,38,0 2,39,0 3,1,0 3,2,0 3,3,0 3,4,0 3,5,0 3,6,0 3,7,0 3,8,0 3,9,0 3,10,0 3,11,0 3,12,0 3,13,0 3,14,0 3,15,0 3,16,0 3,17,0 3,18,1 3,19,0 3,20,0 3,21,0 3,22,0 3,23,0 3,24,0 3,25,0 3,26,0 3,27,0 3,28,0 3,29,0 3,30,0 3,31,0 3,32,0 3,33,0 3,34,1 3,35,1 3,36,0 3,37,1 3,38,1 3,39,1 4,1,1 4,2,1 4,3,1 4,4,1 4,5,1 4,6,1 4,7,1 4,8,1 4,9,1 4,10,1 4,11,1 4,12,1 4,13,1 4,14,1 4,15,1 4,16,0 4,17,1 4,18,1 4,19,1 4,20,1 4,21,1 4,22,1 4,23,1 4,24,1 4,25,1 4,26,1 4,27,1 4,28,1 4,29,1 4,30,1 4,31,1 4,32,1 4,33,1 4,34,1 4,35,0 4,36,0 4,37,0 4,38,0 4,39,0 5,1,0 5,2,0 5,3,0 5,4,0 5,5,0 5,6,0 5,7,0 5,8,0 5,9,0 5,10,0 5,11,0 5,12,0 5,13,0 5,14,0 5,15,0 5,16,0 5,17,0 5,18,0 5,19,0 5,20,0 5,21,0 5,22,0 5,23,0 5,24,0 5,25,0 5,26,0 5,27,0 5,28,0 5,29,0 5,30,0 5,31,0 5,32,0 5,33,0 5,34,0 5,35,0 5,36,0 5,37,0 5,38,0 5,39,0 6,1,0 6,2,0 6,3,0 6,4,0 6,5,1 6,6,1 6,7,1 6,8,1 6,9,0 6,10,0 6,11,0 6,12,0 6,13,0 6,14,0 6,15,0 6,16,0 6,17,0 6,18,0 6,19,0 6,20,0 6,21,0 6,22,0 6,23,0 6,24,1 6,25,1 6,26,1 6,27,1 6,28,0 6,29,0 6,30,0 6,31,0 6,32,0 6,33,0 6,34,0 6,35,0 6,36,0 6,37,0 6,38,0 6,39,0 7,1,0 7,2,0 7,3,0 7,4,1 7,5,0 7,6,0 7,7,0 7,8,0 7,9,0 7,10,0 7,11,0 7,12,0 7,13,0 7,14,0 7,15,0 7,16,0 7,17,1 7,18,1 7,19,1 7,20,1 7,21,1 7,22,1 7,23,1 7,24,1 7,25,0 7,26,0 7,27,0 7,28,0 7,29,0 7,30,0 7,31,0 7,32,0 7,33,0 7,34,0 7,35,1 7,36,1 7,37,1 7,38,1 7,39,1 8,1,1 8,2,1 8,3,1 8,4,1 8,5,0 8,6,0 8,7,0 8,8,0 8,9,0 8,10,0 8,11,0 8,12,0 8,13,0 8,14,0 8,15,1 8,16,1 8,17,0 8,18,0 8,19,0 8,20,0 8,21,0 8,22,0 8,23,0 8,24,0 8,25,0 8,26,0 8,27,0 8,28,0 8,29,0 8,30,0 8,31,0 8,32,0 8,33,0 8,34,1 8,35,1 8,36,0 8,37,0 8,38,0 8,39,0 9,1,0 9,2,0 9,3,0 9,4,0 9,5,0 9,6,0 9,7,0 9,8,0 9,9,0 9,10,0 9,11,0 9,12,0 9,13,0 9,14,1 9,15,1 9,16,0 9,17,0 9,18,0 9,19,0 9,20,1 9,21,1 9,22,1 9,23,1 9,24,1 9,25,1 9,26,0 9,27,0 9,28,0 9,29,0 9,30,0 9,31,0 9,32,0 9,33,1 9,34,1 9,35,1 9,36,1 9,37,1 9,38,1 9,39,1 10,1,1 10,2,1 10,3,1 10,4,1 10,5,1 10,6,0 10,7,0 10,8,0 10,9,0 10,10,0 10,11,1 10,12,1 10,13,1 10,14,1 10,15,1 10,16,1 10,17,1 10,18,1 10,19,1 10,20,1 10,21,0 10,22,0 10,23,1 10,24,1 10,25,0 10,26,0 10,27,0 10,28,0 10,29,0 10,30,1 10,31,1 10,32,1 10,33,1 10,34,1 10,35,1 10,36,1 10,37,0 10,38,0 10,39,0 11,1,0 11,2,0 11,3,1 11,4,1 11,5,1 11,6,0 11,7,0 11,8,0 11,9,0 11,10,1 11,11,1 11,12,0 11,13,0 11,14,0 11,15,1 11,16,1 11,17,0 11,18,0 11,19,0 11,20,0 11,21,0 11,22,1 11,23,1 11,24,1 11,25,0 11,26,0 11,27,0 11,28,0 11,29,0 11,30,0 11,31,0 11,32,0 11,33,0 11,34,1 11,35,1 11,36,0 11,37,0 11,38,0 11,39,0 12,1,0 12,2,0 12,3,1 12,4,1 12,5,0 12,6,0 12,7,0 12,8,0 12,9,0 12,10,0 12,11,0 12,12,0 12,13,0 12,14,0 12,15,0 12,16,0 12,17,0 12,18,0 12,19,0 12,20,0 12,21,0 12,22,1 12,23,1 12,24,0 12,25,0 12,26,0 12,27,0 12,28,0 12,29,0 12,30,0 12,31,0 12,32,0 12,33,0 12,34,0 12,35,0 12,36,0 12,37,0 12,38,0 12,39,0 13,1,0 13,2,1 13,3,1 13,4,0 13,5,0 13,6,0 13,7,0 13,8,0 13,9,0 13,10,0 13,11,0 13,12,0 13,13,0 13,14,0 13,15,0 13,16,0 13,17,0 13,18,0 13,19,0 13,20,0 13,21,0 13,22,0 13,23,0 13,24,0 13,25,0 13,26,0 13,27,0 13,28,0 13,29,0 13,30,0 13,31,0 13,32,0 13,33,0 13,34,0 13,35,0 13,36,0 13,37,0 13,38,0 13,39,0 14,1,0 14,2,0 14,3,0 14,4,0 14,5,0 14,6,0 14,7,0 14,8,0 14,9,0 14,10,0 14,11,0 14,12,0 14,13,0 14,14,0 14,15,0 14,16,0 14,17,0 14,18,0 14,19,0 14,20,0 14,21,0 14,22,0 14,23,0 14,24,0 14,25,0 14,26,0 14,27,0 14,28,0 14,29,0 14,30,0 14,31,0 14,32,0 14,33,0 14,34,0 14,35,0 14,36,0 14,37,0 14,38,0 14,39,0 15,1,0 15,2,0 15,3,0 15,4,0 15,5,0 15,6,0 15,7,0 15,8,0 15,9,0 15,10,0 15,11,0 15,12,0 15,13,0 15,14,0 15,15,0 15,16,0 15,17,0 15,18,0 15,19,0 15,20,0 15,21,0 15,22,0 15,23,0 15,24,0 15,25,0 15,26,0 15,27,0 15,28,0 15,29,0 15,30,0 15,31,0 15,32,0 15,33,0 15,34,0 15,35,0 15,36,0 15,37,0 15,38,0 15,39,0 16,1,0 16,2,0 16,3,0 16,4,0 16,5,1 16,6,1 16,7,1 16,8,1 16,9,0 16,10,0 16,11,0 16,12,0 16,13,0 16,14,0 16,15,0 16,16,0 16,17,0 16,18,0 16,19,0 16,20,0 16,21,0 16,22,0 16,23,0 16,24,1 16,25,1 16,26,1 16,27,1 16,28,0 16,29,0 16,30,0 16,31,0 16,32,0 16,33,0 16,34,0 16,35,0 16,36,0 16,37,0 16,38,0 16,39,0 17,1,0 17,2,0 17,3,0 17,4,0 17,5,1 17,6,1 17,7,1 17,8,1 17,9,0 17,10,0 17,11,0 17,12,0 17,13,0 17,14,0 17,15,0 17,16,0 17,17,0 17,18,0 17,19,0 17,20,0 17,21,0 17,22,0 17,23,0 17,24,0 17,25,1 17,26,1 17,27,1 17,28,0 17,29,0 17,30,0 17,31,0 17,32,0 17,33,0 17,34,0 17,35,0 17,36,0 17,37,0 17,38,0 17,39,0 18,1,0 18,2,0 18,3,0 18,4,0 18,5,0 18,6,1 18,7,1 18,8,0 18,9,0 18,10,0 18,11,0 18,12,0 18,13,0 18,14,0 18,15,0 18,16,0 18,17,0 18,18,0 18,19,0 18,20,0 18,21,0 18,22,0 18,23,0 18,24,0 18,25,1 18,26,1 18,27,0 18,28,0 18,29,0 18,30,0 18,31,0 18,32,0 18,33,0 18,34,0 18,35,0 18,36,0 18,37,0 18,38,0 18,39,0 19,1,0 19,2,1 19,3,1 19,4,1 19,5,1 19,6,1 19,7,1 19,8,0 19,9,0 19,10,0 19,11,0 19,12,0 19,13,0 19,14,0 19,15,0 19,16,0 19,17,0 19,18,0 19,19,0 19,20,0 19,21,1 19,22,1 19,23,1 19,24,1 19,25,1 19,26,1 19,27,0 19,28,0 19,29,0 19,30,0 19,31,0 19,32,0 19,33,0 19,34,0 19,35,0 19,36,0 19,37,0 19,38,0 19,39,0
%% 元データ(org)からExcelデータと同じデータを作る
clear all % 一旦メモリを全てクリアする
org = readmatrix('index.csv'); % 元データ読み込み
org = sortrows(org); % データ取得順がバラバラならソートする % 列優先で[X座標,Y座標,1/0データ]の順
map2 = reshape(org(:,3),[max(org(:,1)),max(org(:,2))]);
writematrix(map2,'zero_one2.csv'); % writematrix(map2,'zero_one2.xls'); 表示の為csvで保存
type zero_one2.csv
0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1 0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1 0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1 0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1 0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,1,1,0,0,1,1,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  3 Comments
Atsushi Ueno
Atsushi Ueno on 18 Dec 2022
>質問の意図が伝わらなくて申し訳ありません
添付の demo1.xlsx 通りのデータを想定していましたよ。質問の意図は明確に伝わっています。
org = readmatrix('https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1234767/demo1.xlsx'); % 元データ読み込み
org = sortrows(org); % データ取得順がバラバラならソートする % 列優先で[X座標,Y座標,1/0データ]の順
map2 = reshape(org(:,3),[max(org(:,1)),max(org(:,2))]);
writematrix(map2,'zero_one2.csv'); % writematrix(map2,'zero_one2.xls'); 表示の為csvで保存
type zero_one2.csv
0,1,1,0,1 0,1,0,1,0 0,0,0,1,0 0,1,1,1,0 1,0,1,0,0
Naoya Oka
Naoya Oka on 18 Dec 2022
ありがとうございます!
上手く進めることができました。

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!