How to perform row wise operation for a matrix of 250x250
Show older comments
Hi, I have a matrix of 250x250 all zeros and ones. Now i have to go through each row and assign 40 to all 1s in the first line until i hit a 0, then i have to go to the second row and assign 40 to all 1s until i hit zero and i have to repeat this until the n-th line is 0, which characterizes the end of the grain. Can anyone let me know the basic idea of coding for the above concept, For an example 5x5 matrix,A=[1 1 1 0 1; 1 1 0 1 1; 1 1 1 0 1; 1 1 0 0 1; 1 1 0 1 0]
Thanks in advance Agastya
Accepted Answer
More Answers (3)
Azzi Abdelmalek
on 21 Nov 2014
A(logical(cumprod(A,2)))=40
Azzi Abdelmalek
on 21 Nov 2014
A=[1 1 1 0 1; 1 1 0 1 1; 1 1 1 0 1; 1 1 0 0 1; 1 1 0 1 0]
for k=1:size(A,2)
idx=strfind(A(k,:),[1 0]);
A(k,1:idx)=40;
end
2 Comments
Thorsten
on 21 Nov 2014
This does not work if the row no 0's.
This is basically the algorithm suggested by Guillaume with an addditional check for the special case where the row has no 0's; note that this only works if a row always starts with a 1.
for i = 1:size(A, 1)
ind = find(A(i, :) == 0);
if isempty(ind)
ind2 = size(A, 2);
else
ind2 = ind(1) - 1;
end
A(i, 1:ind2) = 40;
end
4 Comments
Agastya
on 21 Nov 2014
Guillaume
on 21 Nov 2014
Well, test if the index is 1 and if it is don't replace anything.
As for reading that (?) text file, start a new question. You've never mentioned a text file and it has nothing to do with your initial question.
Agastya
on 21 Nov 2014
Guillaume
on 21 Nov 2014
As I said, start a new question. You'll get more chance of an answer as people won't usually look at questions that have been marked as answered.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!