Unequal vectors in one matrix.

Hello all, i have 4 unequal vectors, i want to place them into a one vector or matrix, and then use each element of each vector in one for loop.
C1=[1 1 1 1 2 2 2 2 1 1];
C2=[0 0 0 0 3 3 3 3 ];
C3=[4 4 4 4 5 5 5 5 6 75 5];
C4=[6 5 6 6 7 7 7 7];
Here in the first loop iteration i want to use the first elements of each which is [1 0 4 6] and then second iteration [1 0 4 5] and so on.

2 件のコメント

Andy
Andy 2020 年 10 月 14 日
What do you expect when the For loop reaches the 9th iteration and the Vectors C2 and C4 are empty.
Mohannad Alzard
Mohannad Alzard 2020 年 10 月 14 日
I'm sorry i was just editing the question i would like to take the minimum number of elements of one of the vector and trim the others to the minimum size and then use the for loop to extract first elements of each vector.

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

回答 (1 件)

KSSV
KSSV 2020 年 10 月 14 日
編集済み: KSSV 2020 年 10 月 14 日

0 投票

C1=[1 1 1 1 2 2 2 2 1 1];
C2=[0 0 0 0 3 3 3 3 ];
C3=[4 4 4 4 5 5 5 5 6 75 5];
C4=[6 5 6 6 7 7 7 7];
iwant = NaN(4,11) ;
iwant(1,1:length(C1)) = C1 ;
iwant(2,1:length(C2)) = C2 ;
iwant(3,1:length(C3)) = C3 ;
iwant(4,1:length(C4)) = C4 ;
Suggested.
C1=[1 1 1 1 2 2 2 2 1 1];
C2=[0 0 0 0 3 3 3 3 ];
C3=[4 4 4 4 5 5 5 5 6 75 5];
C4=[6 5 6 6 7 7 7 7];
C = [{C1}, {C2}, {C3}, {C4}] ;
L = cellfun(@length,C) ;
m = length(C) ;
n = max(L) ;
iwant = NaN(m,n) ;
for i = 1:m
iwant(i,1:length(C{i})) = C{i} ;
end

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2020 年 10 月 14 日

編集済み:

2020 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by