Replacing specific values in each matrix row in all values in the matrix
Show older comments
I have a matrix as follows A=[8 66 92 101 0; 14 52 80 76 0; 16 66 33 51 7]. I want to read the rows column by column and assign all values in the matrix the first value of the row, my code so far works (I have also made an if loop to ignore the zeros), and the output is B=[8 8 8 8 0; 14 14 14 14 0; 16 16 16 16 16], however this isn't exactly what I need. For example, the matrix I need is B=[8 8 8 8 0: 14 14 14 14 0; 8 8 8 8 8] since A(1,2)=66 and this should have already been assigned to the value 8. So what I am looking for is to iterate over each row and column, assign all numbers in the row to the same value throughout the matrix, and whenever the value appears in a row later on the whole row again be assigned to the value, 8, in this example. I hope that this is clear enough. So far, the code is:
F=Locations;
indices = find(F(:,1)==0);
F(indices,:) = [];
for l=1:size(F,1);
for k=1:size(F,2);
if F(l,k)~=0;
[R,L]=find(F==F(l,k));
F(R,L)=F(l,1);
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Resizing and Reshaping 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!