Info

此问题已关闭。 请重新打开它进行编辑或回答。

Duplicating values into other matrix elements

1 次查看(过去 30 天)
I have a 603133x5 logical matrix with a total of 78 true values within it.
For every true value in the first column, I want to rewrite the values in the rows below to true also. The only way I can think of doing this is via a for loop which is, given the size of the matrix, prohibitively time-consuming.
for i=1:length(matches);
if matches(i,1)==1;
macthes(i+1:i+2599)=1;
end
Any guidance of how I can achieve the same result more quickly would be much appreciated.
  2 个评论
Adam
Adam 2014-8-19
I'm not sure I understand the question. If you make every value below a true also true then surely you only need to find the first true in the 1st column and then set every row below that to true which will include all the other cases anyway.
Your example code doesn't seem to match your question. Where does 2599 come from?
Edward
Edward 2014-8-19
Sorry Adam, insufficient precision on my part. Mark of a rookie programmer I guess!
For every row in the first column which =1, I want the following 2599 values to =1.
Thanks

回答(1 个)

Joseph Cheng
Joseph Cheng 2014-8-19
not entirely sure what you mean by "rewrite the values in the rows below to true also" but to do what you're attempting in a faster way would be (if matches is the 603133x5 logical matrix).
det= find(matches(:,1)==1);
for ind = 1:length(det)
matches(det(i):det(i)+2599,:)=1;
end
  1 个评论
Joseph Cheng
Joseph Cheng 2014-8-19
Ah so the next 2599 values are 1; Then a slight modification to my code will be needed. So you know that matlab counts down the columns not across rows you'll have to do some math. so (2599+1)/5 means the next 520 rows are filled with ones. So we modify by:
for ind = 1:length(det)
matches(det(i):det(i)+519,:)=1;
end

Community Treasure Hunt

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

Start Hunting!

Translated by