I have a 3x1536 matrix named time
On the 2nd row I'm trying to fill it in with hours of the day but I need four of each hour so for example:
1 1 1 1 2 2 2 2 3 3 3 3...24 24 24 24 1 1 1 1 2 2 2 2 and so on
.
I have no idea how to go about indexing the matrix in a way that I don't have to write then modify 96 loops/indexing lines of code because I have to fill it out in the only way I can think of right now which is:
for a=1:96:1536
time(2,a)=0;
end
.
for b=2:96:1536
time(2,b)=0;
end
...
for e=5:96:1536
time(2,e)=1;
end

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 14 日
編集済み: Azzi Abdelmalek 2013 年 10 月 14 日

0 投票

Edit
n=1536/4
c2=repmat(1:n,4,1);
time(2,:)=circshift(mod(c2(:)',24)+1,[0 4])
%or
n=1536/(24*4)
a=repmat(1:24,4,1)
time(2,:)=repmat(a(:)',1,n)

1 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 14 日
Look at Edit

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

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2013 年 10 月 14 日

1 投票

time(2,:) = reshape(kron(1:24, ones(16,4)), 1, []);

1 件のコメント

Andrei Bobrov
Andrei Bobrov 2013 年 10 月 14 日
編集済み: Andrei Bobrov 2013 年 10 月 14 日
reshape(kron(1:24, ones(16,4))',1,[])
or
reshape(kron((1:24)', ones(4,16)),1,[])

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

Andrei Bobrov
Andrei Bobrov 2013 年 10 月 14 日

1 投票

reshape(repmat(1:24,[4,1,16]),1,[])
Jos (10584)
Jos (10584) 2013 年 10 月 14 日

0 投票

There are many roads to Rome. Here is another generic and fast solution:
% example data
time = zeros(3,26) % in your case: 3-by-1536
Nval = 6 % in your case : 24
Nrep = 3 % in your case : 4
% engine
time(2,:) = mod(floor((0:(size(time,2)-1))/Nrep), Nval) + 1

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2013 年 10 月 14 日

編集済み:

2013 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by