for 1:x with unkonw x

1 回表示 (過去 30 日間)
Riccardo Rossi
Riccardo Rossi 2019 年 1 月 7 日
コメント済み: Riccardo Rossi 2019 年 1 月 7 日
Hi everybody,
i have an array like this:
ARRAY
X Y Z C
-3 4 5 1
5 6 2 2
4 4 9 1
6 6 7 2
4 6 2 3
9 9 7 3
where C goes from 1 to x (x unknow). I want to create a for to have x arrays with XYZ associated with C, like this:
for k=1:x
A(1:k)=[ARRAY(:,1),ARRAY(:,2),ARRAY(:,3),ARRAY(:,4)==1:k]
end
A1
X Y Z
-3 4 5
4 4 9
A2
X Y Z
5 6 2
6 6 7
A3
X Y Z
4 6 2
9 9 7
Thank you!

採用された回答

madhan ravi
madhan ravi 2019 年 1 月 7 日
編集済み: madhan ravi 2019 年 1 月 7 日
a=[-3 4 5 1
5 6 2 2
4 4 9 1
6 6 7 2
4 6 2 3
9 9 7 3];
u=unique(a(:,4));
A=cell(1,numel(u));
for i = 1:numel(u)
idx = (a(:,4) == u(i)); % edited after Jan's comment
% idx=ismember(a(:,4),u(i));
A{i}=a(idx,1:3);
end
celldisp(A)
Gives:
A{1} =
-3 4 5
4 4 9
A{2} =
5 6 2
6 6 7
A{3} =
4 6 2
9 9 7
  5 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 7 日
編集済み: madhan ravi 2019 年 1 月 7 日
Anytime :)
mean_of_X_Y_Z_in_each_cell=cellfun(@mean,A,'un',0)
Riccardo Rossi
Riccardo Rossi 2019 年 1 月 7 日
So grateful!

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

その他の回答 (1 件)

KSSV
KSSV 2019 年 1 月 7 日
A = [-3 4 5 1
5 6 2 2
4 4 9 1
6 6 7 2
4 6 2 3
9 9 7 3 ] ;
C = A(:,end) ;
[c,ia,ib] = unique(C) ;
N = length(c) ;
iwant = cell(N,1) ;
for i = 1:N
iwant{i} = A(C==c(i),1:3) ;
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by