Arrange cell matrix and get the sum
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
"out" cell matrix has 1x365 cells. I have few questions about dealing with cells.
1) I need to delete cells from 361 to 365 in the "out" cell matrix. How can I do that in MATLAB?
2) After deleting, I need to get the sum like this. For example, from cell 1 - 8.
cell1 cell2 .....cell8 sum
0.0 0.1 0.0 0.1
0.0 0.2 0.5 0.7
0.0 0.1 0.1 0.2
.
.
For example,
for ii=1:8:360
sumout=sum(out{1,ii}{1,3});
end
But, this does not give the sum for all rows. Can someone help me?
Thanks in advance.
0 Commenti
Risposta accettata
Stephen23
il 12 Mar 2015
Modificato: Stephen23
il 12 Mar 2015
Putting lots of scalar values in a cell array is a waste of MATLAB's operation vectorizing abilities and this is why you are finding this task so difficult. I will use a simple numeric array, as this makes the doing these kind of operations much easier. Putting things in cell arrays is what you do when you have to, not for some trivial scalar values like this.
>> A = rand(365,1);
>> B = reshape(A(1:360),[],8);
>> sum(B,2)
ans =
4.4687
3.694
4.5899
3.4787
...
3.5861
3.7248
3.8888
5.2515
This code takes the first 360 values in the numeric array, arrange them into a matrix where each row has eight values. It then sums each row to give the final values in a 45x1 vector. And see how much easier it is to work with numeric arrays rather than cell arrays! You can convert the cell array to a numeric array using cell2mat(X), where X is the cell array.
8 Commenti
Stephen23
il 18 Mar 2015
Modificato: Stephen23
il 18 Mar 2015
In MATLAB addition of two vectors is an element-wise operation: because the third-column of each of file TRMM_1998_01_0XXX_newntcl.csv is a 51681x1 size numeric vector, the end result will also be a single 51681x1 vector. If this is what you want, then you can simply add the data in a loop. If not, then you need to specify how the data should be handled.
This code, based on your earlier comment, shows how you can get the data during each iteration:
filePattern = fullfile(myFolder, '*.csv');
csvFiles = dir(filePattern);
for k = 1:length(csvFiles)
fid(k) = fopen(fullfile(myFolder,csvFiles(k).name));
out{k} = textscan(fid(k),'%s%s%f','delimiter',',');
fclose(fid(k));
dat = out{k}{3};
end
Più risposte (1)
the cyclist
il 12 Mar 2015
% Make up some data that seems to be like yours
rng 'default'
out = num2cell(rand(1,365));
% Convert from cell array to numeric
out_numeric = cell2mat(out);
% Trim the unwanted
trimmed_out = out_numeric(1:360);
% Reshape to 45x8
reshaped_out = reshape(trimmed_out,45,8);
% Sum
summed_out = sum(reshaped_out,2);
1 Commento
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!