Creating Matrix with nested loop values

Hello, I am looking for a solution with my problem storing the output of my nested loop into a matrix. The code works perfectly fine and gives me a row for every looped index. For example :
xm =
2 56 31
xm =
2 57 24.667
What i want:
All rows together in one Matrix. hours,minutes,seconds are double values format %f
zeit = [hours minutes seconds ];
houra=1;
minutea=1;
houre=1;
minutee=1;
for k=2:3
for n=1:60
houra=k-1;
minutea=n-1;
houre=k;
minutee=n;
x=(zeit(zeit(:,1)>=houra & zeit(:,1)<houre & zeit(:,2)>=minutea & zeit(:,2)<minutee ,:));
xm=mean(x);
end
end
I hope you can help me.

 採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 4 月 14 日

1 投票

[a,~,c] = unique(zeit(:,1:2),'rows');
xm = [a, accumarray(c,zeit(:,3),[],@mean)];

3 件のコメント

Peter Krainer
Peter Krainer 2016 年 5 月 2 日
Hello, thanks for your help, it works fine for just the (:,3) value but i have a total of 12 i want to mean and store in the matrix.
Andrei Bobrov
Andrei Bobrov 2016 年 5 月 2 日
Hi Peter! For zeit with size [m x 12]:
[m,n] = size(zeit);
[a,~,c] = unique(zeit(:,1:2),'rows');
[x,y] = ndgrid(c,1:n-2);
A = zeit(:,3:end);
xm = [a, accumarray([x(:),y(:)],A(:),[],@mean)];
Peter Krainer
Peter Krainer 2016 年 5 月 2 日
WOW thank you very much!

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

その他の回答 (1 件)

Jan
Jan 2016 年 4 月 14 日
編集済み: Jan 2016 年 4 月 14 日

0 投票

zeit = ???
size1 = 2 * 60;
xm = zeros(size1, 3);
ixm = 0;
for k = 2:3
hMatch = (zeit(zeit(:,1) >= ha & zeit(:,1) < he);
for n = 1:60
ha = k-1;
ma = n-1;
he = k;
me = n;
mMatch = (hMatch & zeit(:,2) >= ma & zeit(:,2) < me);
ixm = ixm + 1;
xm(ixm, :) = mean(zeit(hMatch & mMatch, :));
end
end

1 件のコメント

Peter Krainer
Peter Krainer 2016 年 5 月 2 日
Thank you for your help very much, but i doesn t work for me.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by