Matrix from for loop displaying vectors of different length
1 次查看(过去 30 天)
显示 更早的评论
Hello, could someone advice. I have wriiten some code (readout.m) that reads a file containing names of 3 data files (filenr.lis). These 3 data files are stored in a directory named wavf. These 3 data files are not of the same length. My code outputs all the 3 datas as vectors of different lengths and stores the results in a variable named output. Unfortunately i have failed to save the 3 results in a matrix whereby the length of the matrix is scaled by the smallest length. I mean trim the datasets such that they have the same lengths and then output the result as a matrix outside the loop. I have attached the datasets in a zip. thx
0 个评论
采纳的回答
KSSV
2020-7-10
You need to interpolate them to the same size using interp1.
clc
clear
close all
[index, sname] = textscan('filenr.lis','%d %s');
N = length(index) ;
iwant = cell(N,1) ;
for k = 1:N
wavfilepath = '\\GSE\\';
sfiles =char(sname(k,:));
s = [wavfilepath sfiles];
fid = fopen(s);
[data] = readfile(fid);
iwant{k} = data{1}; % load data of the file
end
% do interpolatio to make them to same size
L = cellfun(@length,iwant) ; % length of each array
Lmax = max(L) ;
data = zeros(L,N) ;
for i = 1:N
x = 1:length(iwant{i}) ;
y = iwant{i} ;
xi = linspace(1,length(iwant{i}),Lmax) ;
data(:,i) = interp1(x',y,xi') ;
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!