Hello!
I wrote this code:
X=rand(5,3);
Y=zeros(5,3);
for k=1:5
for j=1:3
if X(k,j)<0.2
Y(k,j)=-1;
else
Y(k,j)=1;
end
end
end
I have to write the same code but without using any loop. How to do this?

1 件のコメント

Cedric
Cedric 2019 年 4 月 28 日
Google "MATLAB logical indexing" and understand it, and you'll have the tools for solving your problem.

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

 採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 4 月 28 日
編集済み: KALYAN ACHARJYA 2019 年 4 月 28 日

0 投票

%Though I am giving the answer here, but please must follow the Google "MATLAB logical indexing" as stated by @Cedric
There are multiple ways, you can do this. One way-
X=rand(5,3);
idx=X<0.2;
Y=ones(5,3);
Y(idx)=-1

3 件のコメント

Cedric
Cedric 2019 年 4 月 28 日
編集済み: Cedric 2019 年 4 月 28 日
You can eliminate a few things:
X = rand(5,3);
idx = X < 0.2;
Y = ones(5,3);
Y(idx) = -1
also, Pawel, display idx and evaluate class(idx).
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 4 月 28 日
編集済み: KALYAN ACHARJYA 2019 年 4 月 28 日
Exactly Yes @Cedric sir, I supposed X and Y are given, therefore I replaced original Y with ones.
Thanks!
pawlo392
pawlo392 2019 年 4 月 28 日
Thank You for help. :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLanguage Support についてさらに検索

質問済み:

2019 年 4 月 28 日

コメント済み:

2019 年 4 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by