Info

This question is closed. Reopen it to edit or answer.

Duplicating values into other matrix elements

1 view (last 30 days)
Edward
Edward on 19 Aug 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comments
Adam
Adam on 19 Aug 2014
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 on 19 Aug 2014
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

Answers (1)

Joseph Cheng
Joseph Cheng on 19 Aug 2014
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 Comment
Joseph Cheng
Joseph Cheng on 19 Aug 2014
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!