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.
采纳的回答
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 个评论
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
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
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
