Any hint about this error?

Steps to reproduce (on Matlab R2013a 64bit Win machine):
R = rand(7,7);
>> ind_diag = eye(7);
>> R(ind_diag)
Error: Subscript indices must either be real positive integers or logicals.
>> ind_diag = ~~eye(7);
>> R(ind_diag)
ans =
0.8702
0.3818
0.6567
0.1098
0.4873
0.1401
0.7657
Cheers, Chris

댓글 수: 1

Jos (10584)
Jos (10584) 2013년 7월 16일
logical(x) is faster, and easier to read!, than ~~x

댓글을 달려면 로그인하십시오.

 채택된 답변

Chris
Chris 2013년 7월 16일

0 개 추천

thank you Harjeet,
however i still wonder why eye() or ones() don't work.
Is it a bug or a misuse of these functions?

댓글 수: 2

Take a look at this
a = 1
b = logical(a)
class (a)
class(b)
Chris
Chris 2013년 7월 16일
thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

harjeet singh
harjeet singh 2013년 7월 16일

0 개 추천

use this
R = rand(7,7);
ind_diag = logical(eye(7));
R(ind_diag)
harjeet singh
harjeet singh 2013년 7월 16일

0 개 추천

eye() will give you a output in double format and when you use ~~eye() it automatically converts in logical.
the command R(ind_diag) uses inputs as logical so that's why you have to convert it into logical before use

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2013년 7월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by