index info for CAT

1 回表示 (過去 30 日間)
xplore29
xplore29 2014 年 8 月 11 日
コメント済み: Azzi Abdelmalek 2014 年 8 月 11 日
I am concatenating vectors x1,x2,x3 using y=cat(2,x1,x2,x3). Each one of these vectors have different lengths. I need to keep track index information for each one of them. How can I do this without loops.
x1=[1 1 0 1]; x2=[1 0]; x3=[1 1 1];
y=cat(2,x1,x2,x3);
= [1 1 0 1 1 0 1 1 1]
index = [1 4;5 6;7 9]

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 8 月 11 日
編集済み: Azzi Abdelmalek 2014 年 8 月 11 日
index=reshape(cumsum([1 numel(x1)-1 1 numel(x2)-1 1 numel(x3)-1]),2,[])'
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 8 月 11 日
Instead of using 3 variables x1,x2 and x3 you can use a single variable
x={[1 1 0 1];[1 0];[1 1 1]} % x is a cell array
you can get your three variables
x{1}
x{2}
x{3}
% Now you can get y
y=cell2mat(x')
out=reshape(cumsum([1 numel(x1)-1 1 numel(x2)-1 1 numel(x3)-1]),2,[])'

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by