Given:
w=[2,8,3,30,4,50,100,200,4,80,500]
How can I turn the following into a single line of code?
r=w(w>0 & w<10)
s=w(w>10 & w<100)
t=w(w>100 & w<1000)
I tried variations of:
[r,s,t]=w(w>0 & w<10),w(w>10 & w<100),w(w>100 & w<1000)

댓글 수: 2

Stephen23
Stephen23 2021년 1월 26일
편집: Stephen23 2021년 1월 26일
@Don Kelly: I removed all of those ```and ´´´ characters, and formatted your code correctly by simply selecting the text and clicking the CODE button.
Don Kelly
Don Kelly 2021년 1월 28일
편집: Don Kelly 2021년 1월 28일

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

 채택된 답변

weikang zhao
weikang zhao 2021년 1월 26일

1 개 추천

Use anonymous functions, it allows you to implement quite complex functions in one line. MATLAB supports dot indexing into function call results, as in foo(arg).prop. Other forms of indexing into function call results (with parentheses such as foo(arg)(2) or with curly braces such as foo(arg){2}) are not supported. So, I used feval and anonymous functions to complete this function in disguise.
w=[2,8,3,30,4,50,100,200,4,80,500];
[r,s,t]=feval(@(x) x{:},arrayfun(@(a,b) w(w>a&w<b),[0,10,100],[10,100,1000],'UniformOutput',false));
have fun!

댓글 수: 3

Don Kelly
Don Kelly 2021년 1월 26일
Thank you. There is much to unpack from this function that I am not familiar with.
feval is a function that just evaluates all functions passed to it.
idk what x is in this instance or why you use case array parens then selecting every element in the row
then you us @ but I don't see the handle that is created by using @, unless the handle is (x) and (a,b)
arrayfun is a function that applies the functionality passed to every element of the array so i guess you applied @(a,b) to every element in w where w is greater than a and less than b, but then how does arrayfun know the ranges to perform the function @(a,b) on?
It looks like your arrays [0,10,100] and [10,100,1000] must be what the @(a,b) function looks to for range but i dont get it.
why di you need to specify that the output should not return uniformly?
Don Kelly
Don Kelly 2021년 1월 26일
I guess also is there a less advanced way?
An anonymous function does not need to name the function handle, you can destroy it in place after using it like I did. You can view the output of arrayfun&`feval`, this will help you understand. `arrayfun` can apply function to each element of array, so
arrayfun(@(a,b) w(w>a&w<b),[0,10,100],[10,100,1000],'UniformOutput',false)
will get a cell array and three matrices are stored separately.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 1월 26일

2 개 추천

[r,s,t] = deal(w(w>0 & w<10),w(w>10 & w<100),w(w>100 & w<1000))

댓글 수: 1

Don Kelly
Don Kelly 2021년 1월 28일
Thank you Walter,
I accepted weikang zhao's answer since it was functional and first. I wanted to say that I am really grateful for your answer as well.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2021년 1월 26일

편집:

2021년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by