Normalize matrices of different sizes

2 ビュー (過去 30 日間)
xiao wei
xiao wei 2020 年 11 月 5 日
コメント済み: xiao wei 2020 年 11 月 11 日
I have different size matrix a1, a2, a3...an, size of a1= [m n1]; size of a2=[m n2]; size of a3=[m n3];...size of an=[m nn]; this means that the row number of all the matrix is the same, but their columns number is different. How could I normalize them into matrix [m n], while n is the biggest column number of all matrix? the rest of colums of other matrix should be added with zero. finally, how can I save all new matrix into a 3-D matrix?

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 11 月 5 日
See this example
a1 = rand(3,4);
a2 = rand(3,7);
a3 = rand(3,2);
a4 = rand(3,5);
a = {a1, a2, a3, a4};
m = height(a{1});
n = max(cellfun(@width, a));
a = cellfun(@(x) {[x zeros(m, n-width(x))]}, a);
A = cat(3, a{:}) % 3D matrix
  3 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 11 月 9 日
Can you show your code?
xiao wei
xiao wei 2020 年 11 月 11 日
my code is as follows:
loadstr(:,1) ={'...'}; %file name
loadstr(:,2) = {... }; % file classification
for i = 1:numel(loadstr(:,1))
path = 'C:\...'; % path for file
file = loadstr{i,1};
fileName = strcat(path,file,'.mat');
vars{i} = load(fileName);
tmp (i) = structfun(@length,vars{i});
maxLength=max(tmp);
end
for i = 1:numel(loadstr(:,1))
a = cellfun(@(x) {[x zeros(126, maxLength-tmp(i))]}, vars(i));
end
my problem is:
  1. how can I add zeros(126, maxLength-tmp(i)) to the end of each file and save it as new file (in the name of old file)?
  2. how can I save these new files into a 3-D matrix?

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by