i am having a matrix and want to separate them in the depending on its value
A=[1,2,3,4,5,6,7,8,9,10]
expected result are
idx1=[1,0,0,0,0,0,0,0,0,0] % for 1
...
idx10=[0,0,0,0,0,0,0,0,0,1] % for 10

 採用された回答

Andrei Bobrov
Andrei Bobrov 2018 年 10 月 25 日

1 投票

A = 1:10;
idx = A(:) == A(:)'

2 件のコメント

Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 25 日
what should i change to have a 5 rows with a step of 2
Andrei Bobrov
Andrei Bobrov 2018 年 10 月 25 日
what should i change to have a 5 rows with a step of 2
?
A = 1:2:10;
idx = A(:) == A(:)';

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 10 月 24 日
編集済み: madhan ravi 2018 年 10 月 24 日

1 投票

A=[1,2,3,4,5,6,7,8,9,10]
RESULT = zeros(1,numel(A));
RESULT1= RESULT;
for i = 1:numel(A)
idx(i)=A(i)==1;
idx1(i)=A(i)==10;
end
RESULT(idx) = A(idx)
RESULT1(idx1) = A(idx1)

10 件のコメント

madhan ravi
madhan ravi 2018 年 10 月 24 日
No need to use logical indexing , see the above illustration
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 24 日
how can form a loop for this?
madhan ravi
madhan ravi 2018 年 10 月 24 日
See the edited answer ,problem solved!
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 24 日
編集済み: Shubham Mohan Tatpalliwar 2018 年 10 月 25 日
A=[1,2,3,4,5,6,7,8,9,10]
idy=zeros(1,length(A));
for i=1:1:10
idx22=find(A >=(i) & A<(i+1));
idy(i,idx22)=1;
end
madhan ravi
madhan ravi 2018 年 10 月 24 日
編集済み: madhan ravi 2018 年 10 月 24 日
hows this related to the question??? what did you do in the above comment?
Shubham Mohan Tatpalliwar
Shubham Mohan Tatpalliwar 2018 年 10 月 25 日
編集済み: Shubham Mohan Tatpalliwar 2018 年 10 月 25 日
i have created a array
in which at evry row the numbers are indexed in a step of 1
from this array i can use evry row as a vector
and important is that, its done in a loop so i do not have to repeat the operation 100 times
try the updated code to get a clear idea
madhan ravi
madhan ravi 2018 年 10 月 25 日
I know but you don’t have Use a loop
madhan ravi
madhan ravi 2018 年 10 月 25 日
A=[1,2,3,4,5,6,7,8,9,10]
idy=zeros(1,length(A));
for i=1:1:10
idx22=find(A >=(i) & A<(i+1));
idy(i,idx22)=1;
end
madhan ravi
madhan ravi 2018 年 10 月 25 日
what do you mean by the above code?
madhan ravi
madhan ravi 2018 年 10 月 25 日
What should be the result after the loop?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by