merge two cells to one in loop

1 次查看(过去 30 天)
Joost de Witte
Joost de Witte 2020-1-15
Hello all,
I've got two cells A and B. Cell A and B are the exact same size, however within A and B the vector length of the arrays differ. Cell A and B are produced by a loop which extracts data from 10 datasets using data.values, which I then edit and put into cells. I now want to add both of these cells together to get a third cell (C) which is the same size of A and B (so every value in A gets summed with the overlaping value of B). What is the most novice way of doing this?Thanks in advance
for i=1:10
Adata{i}=data.values(:,192);
Araw = cellfun(@abs,Araw,'UniformOutput',false);
Bdata{i}=data.values(:,193);
Araw = cellfun(@abs,Braw,'UniformOutput',false);
A = cellfun(@(x) x*1250/100,Araw,'UniformOutput',false);
B = cellfun(@(x) x*1250/100,Araw,'UniformOutput',false);
end

回答(1 个)

Prabhan Purwar
Prabhan Purwar 2020-1-20
Hi,
Following code may help
clc
close all
clear
data.values=ones(20,200);
for i=1:10
A{i}=data.values(:,192);
B{i}=data.values(1:10,193);
a=A{i};
b=B{i};
sx=size(a,1);
sy=size(b,1);
amin=min(sx,sy);
if sx>sy
S{i}=[a(1:amin)+b(1:amin);a(amin+1:end)];
elseif sx<sy
S{i}=[a(1:amin)+b(1:amin);b(amin+1:end)];
else
S{i}=[a(1:amin)+b(1:amin)];
end
end
Here I have used simple addition of cell array elements in a one-to-one manner, although the same could be achieved using cellFun().

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by