Can someone check my code ?

Hello!
I have a binary matrix A (m*n),in each row i want to keep arbitrary just '1' in order to get sum in each row ="1"
at first i selected the last non zero value in each row with this code :
[rows, columns] = size(A)
% Create an array to keep track of the column of the last 1 in each row.
lastNonZeroColumn = zeros(rows, 1);
% Loop over rows, finding the last 1 in each row.
for row = 1 : rows
% Find the last 1 in this row, if any exist.
col = find(A(row, :), 1, 'last');
if ~isempty(col)
% At least one 1 exists. Log it's location.
lastNonZeroColumn(row) = col;
end
end
% Display results in command window:
lastNonZeroColumn
after this i picked out just '1' except the last non zero value, i used this code :
N=zeros(size(A);
for k=1:size(A,1)
index=find(A(k,1:lastNonZeroColumn(i)-1));
if isempty(index),continue;end
select=randperm(length(index),1);
N(k,index(select))=1;
end
but i still get '1' at the position of last non zero value.
can anyone help me please!
thanks in advance.

4 Comments

So in order to understand what you try to do:
You have a binary matrix. For each row, you determine the last column with '1' as entry.
Then for each row, you find the columns < the last column with '1' as entry, choose arbitrarily one such column and write a '1' at this position of a matrix N which was initialized as 0-matrix.
What do you want to change in this procedure ?
Note that if A has only one '1' in some row, you risk that this row remains zero in the matrix N.
in the matrix that I have,each row there are several '1', I want to select only '1' except the last '1' (the last position of "1" in each row ,not the last column), if a row has only '1' and it is in the last position, I don't select it. it may exist that row has sum =zero
(Not relevant to this question, but in order to get this message through to the poster who deleted a question earlier:)
With regards to the summation you asked about:
R in your equation appears to be a fixed value. You are adding the fixed value together T times,
R %t = 1 to 1
R+R = 2*R %t = 1 to 2
R+R+R = 3*R %t = 1 to 3
and so on.
And you are wanting to compare that matrix sum to the scalar value k.
  • if k is 0 then D = 0, since sum t=1:0 of something is 0 as no elements would be added
  • if k is > 0 and any R>0 then D = ceil(max(R(:))/k)
  • if k is negative then D = 0 since as we showed above the empty sum is 0 and negative < 0
  • if k is > 0 and all R<=0 then there is no solution
The analysis would be quite different if the equation were and it would be different again if it were something like
Maria
Maria on 16 Sep 2022
Edited: Maria on 16 Sep 2022
@Walter Roberson I really appreciate your effort, thank you so much for answering me here, but I tried to find another solution that's why I deleted the question.

Sign in to comment.

 Accepted Answer

Torsten
Torsten on 11 Sep 2022
Moved: Cris LaPierre on 11 Sep 2022
That's exactly what your code does in my opinion.
Or give an example where it does not perform as you described it should.
Found a mistake in your code:
index=find(A(k,1:lastNonZeroColumn(i)-1));
should of course be
index=find(A(k,1:lastNonZeroColumn(k)-1));

4 Comments

Maria
Maria on 11 Sep 2022
Moved: Cris LaPierre on 11 Sep 2022
it's perfectly right ,thank you for your answer!
Torsten
Torsten on 11 Sep 2022
Moved: Cris LaPierre on 11 Sep 2022
And it works now as it should ?
Maria
Maria on 11 Sep 2022
Moved: Cris LaPierre on 11 Sep 2022
@Torsten of course! many thanks
@Maria please click the "Accept this answer" link to award @Torsten his "Reputation points" for helping you. Thanks in advance. 🙂

Sign in to comment.

More Answers (0)

Categories

Asked:

on 11 Sep 2022

Edited:

on 16 Sep 2022

Community Treasure Hunt

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

Start Hunting!