merge two cells to one in loop

조회 수: 1 (최근 30일)
Joost de Witte
Joost de Witte 2020년 1월 15일
답변: Prabhan Purwar 2020년 1월 20일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by