I want to index a element of a vector after defining it

조회 수: 1 (최근 30일)
Eduardo
Eduardo 2024년 10월 7일
이동: Rik 2024년 10월 7일
I want to select the first element of a vector without having to store it in a variable. For example, if i want to know the smallest prime divider of a number, i want to write this
factor(56)(1)
However this is invalid. Any correct way to do this?

채택된 답변

Stephen23
Stephen23 2024년 10월 7일
이동: Rik 2024년 10월 7일
"Any correct way to do this?"
Yes, but only if you want to make your code complex, awkward, and obfuscated:
subsref(factor(56),substruct('()',{1}))
ans = 2
struct('x',factor(56)).x(1)
ans = 2
The MATLAB approach: store it in a vector. Note that how many lines you use is not an indicator of runtime efficiency.
  댓글 수: 1
Eduardo
Eduardo 2024년 10월 7일
이동: Rik 2024년 10월 7일
Thanks, i guess i will store it in a vector

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by