Arrange cell matrix and get the sum

2 ビュー (過去 30 日間)
Damith 2015 年 3 月 12 日

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?

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

採用された回答

Stephen 2015 年 3 月 12 日

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 件のコメント表示非表示 7 件の古いコメント
Stephen 2015 年 3 月 18 日
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

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

その他の回答 (1 件)

the cyclist 2015 年 3 月 12 日
% 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 件のコメント表示非表示 なし
Damith 2015 年 3 月 12 日
my out is a cell matrix which I obtained through this:
myFolder = 'C:\Users\Desktop\test';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
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));
end
cell2mat(out) does not work for me. I get an rrror message: (See the image of my cell matrix)
Error using cell2mat (line 52)
Cannot support cell arrays containing cell arrays or
objects. サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!