Array value replace when change in row

I have one row of matrix
A = [ 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ]
the row has only two value 0 and 1. I want the code that will check the row if vaule change in row it will leave the first value change and aftre that it puts 0.
Output ::
A = [ 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 1 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ]
Here you can see value of row is change at (13,1) position so it leave the first 1 and remaing 1 is replaced by 0.
Note::: this is example actual row size is 1508 x 1.
Can some one please help me thank you.

 Accepted Answer

A=randi([0,1],7,10); %Example
[m,n]=size(A);
[~,idx]=max(A,[],1);
out = sparse(idx,1:n,1,m,n) ;
A,
A = 7×10
1 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 1 0 1 0 0 1 1 0 0 0 0 1 1 1 1 1 1 0 1 0 0 1 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 0 1 1 0 1 1
full(out)
ans = 7×10
1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

12 Comments

Is showing me this error: can you help me in this?
Thanks...
Error using sparse
Index exceeds array bounds.
Error in Untitled (line 4)
out = sparse(idx,1:m,1,m,n) ;
I fixed it.
Thank you boss
your answer this right.
But if I want to apply this code on particular row let say row no 3, 5, 7, 9.
and remaing row like 1, 2, 4, 6, 8, 10 stay same without change.
and one more help sir if I want apply this in same way but,
example
A = [0 ; 0 ; 0 ; 0 ; 0 ; 1 ; 1 ; 1 ; 1 ; 1 ; 1 ; 0 ; 0 ; 0 ; 0 ; 0 ; 1 ; 1 ; 1 ; 1]
And my answer is like
A = [0 ; 0 ; 0 ; 0 ; 0 ; 1 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 1 ; 0 ; 0 ; 0]
So, here you can see it allow the first change and do accoding to the code if afer some zeros 1 agian comes it leaves than its replace 1 with 0s. this just example but change happens ramdomly so the code work for that one as well for m x n matrix.
Thanks for your help....
Thank you boss
Matt J
Matt J on 18 Oct 2020
Edited: Matt J on 18 Oct 2020
But if I want to apply this code on particular row let say row no 3, 5, 7, 9. and remaing row like 1, 2, 4, 6, 8, 10 stay same without change.
Do you mean rows or do you mean columns? My example has10 columns but only 7 rows, so there is no row 8,9,10.
Columns sir
I want to apply this on columns sir
[m,n]=size(A);
A=diff([zeros(1,n);A])>0
Sir this one is not working as I want:
% Input that I gave
A = 7×10
% C1 C2 C3 C4 C5 C6 C7 C8 C9 C10
1 0 0 1 1 0 0 0 0 0
0 1 1 0 1 0 1 0 1 1
1 1 1 0 1 0 1 1 0 1
0 0 1 1 0 0 0 0 1 1
1 1 1 1 0 1 0 0 1 0
0 0 1 1 1 0 1 1 0 0
1 1 1 0 0 1 1 0 1 1
full(out)
% Output that I am looking for it :
ans = 7×10
% C1 C2 C3 C4 C5 C6 C7 C8 C9 C10
1 0 0 1 1 0 0 0 0 0
0 1 1 0 0 0 1 0 1 1
1 1 1 0 0 0 0 1 0 1
0 0 1 1 0 0 0 0 0 1
1 1 1 1 0 1 0 0 0 0
0 0 1 1 0 0 0 1 0 0
1 1 1 0 0 1 0 0 0 1
% Here you can see change happnes only at column no. 5, 7, 10 remaing stay
% same as input
Can you please help me thanks sir
You could just overwrite the output with the original A
out(:,[5,7,10])=A(:,[5,7,10])
% I used this code but this is change my whole matrix.
A=randi([0,1],7,10); %Example
[m,n]=size(A);
[~,idx]=max(A,[],1);
out = sparse(idx,1:n,1,m,n) ;
A,
full(out)
out(:,[5,7,10])=A(:,[5,7,10])
% What I want is column 1,2,3,4,6,8 and 10 remain same as my input matrix without
% any change
% but my column 5,7 and 9 will change accoding to the code like it will allow first
% one in the column 5,7 and 9 and the second 1 become 0s if it comes.
% and my cloumn 1,2,3,4,6,8 and 10 remain same as my input matrix without
% any change
% can please look into this, I am new in matlab so I don's know about i
% THANKS -V
I showed you how to restore columns 5,7,10. You can use the same method to restore any other columns you wish.
yes sir I tried but its not working run the command sir you will get all idea thanks
I got my answer thanks for you help.......
you are really helpfull

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018a

Asked:

on 17 Oct 2020

Commented:

on 19 Oct 2020

Community Treasure Hunt

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

Start Hunting!