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 件のコメント
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
2020 年 10 月 18 日
I fixed it.
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
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
2020 年 10 月 18 日
Columns sir
I want to apply this on columns sir
Matt J
2020 年 10 月 18 日
[m,n]=size(A);
A=diff([zeros(1,n);A])>0
Vivek Shukla
2020 年 10 月 19 日
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
Matt J
2020 年 10 月 19 日
You could just overwrite the output with the original A
out(:,[5,7,10])=A(:,[5,7,10])
Vivek Shukla
2020 年 10 月 19 日
% 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
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
2020 年 10 月 19 日
yes sir I tried but its not working run the command sir you will get all idea thanks
Vivek Shukla
2020 年 10 月 19 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
