Problem with vertically Concatenating vectors in a function

1 回表示 (過去 30 日間)
Joel Schelander
Joel Schelander 2021 年 4 月 27 日
コメント済み: Joel Schelander 2021 年 4 月 28 日
I have a cell array ABC containing three cells each cell contains a 1x1000 double. What I want is to put every double on top of each other in a column vector (nx1).
ABC={1x1000 cell
1x992 cell
1x928 cell);
My function looks like this:
nr=3;
s=1000;
[D]=func(nr,ABC,s);
function D = func(nr,ABC,s)
D = [];
for y =1:nr
data_length = length(ABC{y});
for o = 1:data_length
G((o-1)*s+1:o*s) = [ABC{y}{o} zeros(1,s-length(ABC{y}{o}))].';
end
D = [D; G.'];
end
end
My output should be D=2920000x1 double
But my output now is D=3000000x1 double

採用された回答

Stephen23
Stephen23 2021 年 4 月 28 日
編集済み: Stephen23 2021 年 4 月 28 日
Rather than using a loop, a much better use of MATLAB would be to use comma-separated lists:
For example:
out = cellfun(@(a)a(:),[ABC{:}],'uni',0);
out = vertcat(out{:})
Or
out = [ABC{:}];
out = [out{:}].'
  2 件のコメント
Stephen23
Stephen23 2021 年 4 月 28 日
"Since every element is a double of 1x1000..."
Lets check that first. Show the output of these commands:
vec = cellfun(@numel,[AAG{:}]);
find(vec~=1000)
Joel Schelander
Joel Schelander 2021 年 4 月 28 日
Ye its not sorry. Cell 3 looks like this
AAG{3,1}=1x757 double 1x726 double 1x761 double 1x785 double...]
The thing is that each of the doubles have a representing value in another cell array.
AAI{3,1}=[8 11 8 11...]
What I really want to achieve is to plot AAI against AAG.. Thus I need to repeat the values of AAI so that it corresponds to the doubles in AAG.

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

その他の回答 (1 件)

DGM
DGM 2021 年 4 月 27 日
% a cell array of numeric row vectors
A = {1:3,10:14,1:5}
% all row vectors concatenated into a col vector
bigcolvec = [A{:}]'
gives
A =
1×3 cell array
{1×3 double} {1×5 double} {1×5 double}
bigcolvec =
1
2
3
10
11
12
13
14
1
2
3
4
5
  1 件のコメント
Joel Schelander
Joel Schelander 2021 年 4 月 28 日
Now the vectos from the cells cant be concatenated:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by