How to perform row wise operation for a matrix of 350x350
Show older comments
Hi, I have a matrix of 350x350 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 and i have to continue the same concept for the remaining numbers i.e now i have to again start with first row but this time from the place where i have left it last time i.e. the number(1) after zero should replace with some random number other than 40 and i have to continue the same concept for 350x350 matrix
for example if A=[1 0 1 0 1; 1 0 1 0 1;1 0 0 0 0; 0 0 0 1 1;1 1 0 0 0] it should change to A=[40 0 50 0 60; 40 0 50 0 60; 40 0 0 0 0 ;0 0 0 70 70; 80 80 0 0 0];
From the code below i could be able to change the values for initial rows but i have no idea how to proceed further so, can anyone help me with this and the code below is only working if the row starts with 1 but i have some rows where it is starting with 0s (if the row is starting with 0 it should be neglected)
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
Thanks in advance Agastya
1 Comment
Image Analyst
on 24 Nov 2014
Reference (original question): http://www.mathworks.com/matlabcentral/answers/163722-how-to-perform-row-wise-operation-for-a-matrix-of-250x250
Accepted Answer
More Answers (1)
Matt J
on 24 Nov 2014
Insert
if ~A(i,1), continue; end
Categories
Find more on Operating on Diagonal 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!