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 Exchange에서 Language 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