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.

 채택된 답변

Matt J
Matt J 2020년 10월 18일
편집: Matt J 2020년 10월 18일

0 개 추천

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

Vivek Shukla
Vivek Shukla 2020년 10월 18일
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) ;
Matt J
Matt J 2020년 10월 18일
I fixed it.
Vivek Shukla
Vivek Shukla 2020년 10월 18일
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 2020년 10월 18일
편집: Matt J 2020년 10월 18일
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.
Vivek Shukla
Vivek Shukla 2020년 10월 18일
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
Matt J
Matt J 2020년 10월 19일
I showed you how to restore columns 5,7,10. You can use the same method to restore any other columns you wish.
Vivek Shukla
Vivek Shukla 2020년 10월 19일
yes sir I tried but its not working run the command sir you will get all idea thanks
Vivek Shukla
Vivek Shukla 2020년 10월 19일
I got my answer thanks for you help.......
you are really helpfull

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

2020년 10월 17일

댓글:

2020년 10월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by