How do I replace cell in specific column

Hi, I have matrix n*n, let's say 4*4
0 1 0 1
1 1 1 0
1 0 1 1
0 1 0 1
And I want change the values of 0 to 1 (just 3 zeros maximum) from the first and second and fourth column (I can not change from third column)
It will becomes
1 1 0 1
1 1 1 1
1 1 1 1
0 1 0 1
So that's what I should to got, my problem is too complicated so I have if condition if the cell is out of specific part and it is zero, then change it..
if numZeros1 > numZeros2
diff = numZeros1 - numZeros2; %for example = 3
for i=1: diff
if offspring1(:, [1:crossoverPoint1-1 crossoverPoint2:end]) == 0 %to change out of portion which is here the third column
end
end
This what I tried but I can't complete it and I think it is wrong :(
To summary, I want to change specific number of 0 to 1 for specific portion in a matrix, how can I do it please?

5 Comments

I can say I really understand what you mean. How do you determine which zeros should be changed to a one? Should the solution assume you know which column should be left alone, or should that be determined?
Something that might be a nice step towards a solution: if you need to fill the zeros from the top, you can actually use find. If you transpose your matrix, you can use find to find the first k linear indices.
Thank you mr. Wisselink, I really appreciate you..
About the zeros, there is no role acully except not inside blocked portion (as you can see, I used crossoverPoint1 and crossoverPoint2 to determine this portion), and no somthing I have to tell you about, it can be more than one column I can not change from it, just in this example i used one column, but in fact it can be more than one and yes I determined it, it is from crossoverPoint1 and crossoverPoint2..
and no somthing I have to tell you about
Let us be the judge of that. Your example is so trivial, that
M = [0 1 0 1
1 1 1 0
1 0 1 1
0 1 0 1] %your demo input
M(:, [2 4]) = 1
would get your desired output. So give us as much details as possible so we can give you an answer that actually do what you want.
I tried to gave you details as much as I can but I will try again, let us assume we have 7*7 matrix
A = [0 1 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 1 0 1 1
1 0 1 1 1 1 1
1 1 1 1 0 1 0
1 1 0 1 1 1 1
0 1 1 1 1 1 1]
And crossoverPoint1 = 3, crossoverPoint2 = 6. and I want to change 4 zeros outside this portion (from column 3 to 6) it will become:
A = [1 1 1 0 1 0 1
1 1 1 1 1 1 1
1 1 1 1 0 1 1
1 1 1 1 1 1 1
1 1 1 1 0 1 1
1 1 0 1 1 1 1
0 1 1 1 1 1 1]
as you can see, A(1, 1), A(1, 7), A(4, 2) and a(5, 7) are changed from zero to one..
I hope this example helps you to understand me, thank you so much..
But you're changing more than 3 zeros. You said "(just 3 zeros maximum)" and now you're changing 4 zeros to 1s. Why???
And why did the second zero in the last column change to 1, but not the second zero in the first column???
And again, what is the overall context -- the "use case"?

Sign in to comment.

 Accepted Answer

Now that you've explained clearly what you want:
A = [0 1 1 0 1 0 0
1 1 1 1 1 1 1
1 1 1 1 0 1 1
1 0 1 1 1 1 1
1 1 1 1 0 1 0
1 1 0 1 1 1 1
0 1 1 1 1 1 1] %demo matrix
crossoverPoint1 = 3
crossoverPoint2 = 6
replacecount = 4;
A = A.'; %swap row columns as you seem to prioritise rows over columns
Adup = A; %to keep original matrix
Adup(crossoverPoint1:crossoverPoint2, :) = 1; %don't care about 0s in these rows (originally columns). Set everything to 1
A(find(Adup == 0, replacecount)) = 1; %replace the first replacecount 0s by 1s.
A = A.' %transpose back

2 Comments

This changes more than 3 zeros. It changes 4 zeros. In the original question he said "just 3 zeros maximum" however he did seem to break that requirement later in the comments for some reason. It's inconsistent to me.

Sign in to comment.

More Answers (1)

This simple and intuitive for loop will do it.
m = [0 1 0 1
1 1 1 0
1 0 1 1
0 1 0 1]
[rows, columns] = size(m)
changeCount = 0;
% Change the first zero in each column
% except column 3, and quit after
% 3 zeros have been changed.
for col = 1 : columns
if col == 3
continue; % Skip column 3 for some reason.
end
for row = 1 : rows
if m(row, col) == 0
m(row, col) = 1;
changeCount = changeCount + 1;
% Now that we've changed one in this column,
% skip to the next column.
break;
end
end
if changeCount == 3
break;
end
end
m % Show in command window.
By the way, why do you want to do this quirky thing? What's the "use case"?

Categories

Asked:

S A
on 23 Jan 2019

Commented:

on 24 Jan 2019

Community Treasure Hunt

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

Start Hunting!