필터 지우기
필터 지우기

How EFFICIENTLY to extract multiple column base on multiple condition

조회 수: 1 (최근 30일)
balandong
balandong 2017년 11월 30일
댓글: balandong 2017년 11월 30일
Dear all, The objective was extract the data from s.c2. Depending on the binary state (either 1 or 2) of s.state, the data can be extracted either from either column ONE or Two of the s.c2. To achieve the objective, the following code were realized. However, the code is ineffective. In other word, the number of Function (EXTRACT_COL) calling increase with number of COLUMNX.
I wonder if there any possible way to optimize this code further. For example, we can input all the binary state vector and get the respective s.c2 column all at once. That is, do the procedure in parallel.
I really appreciate for any advice or tips.
Thanks in advance
Columnx=10;
s.c2 = randi(10,4,2);
s.state=randi([1 2],1,Columnx);
[r]= arrayfun(@(k) extract_Col(s,k),1:6,'un',0);
function r=extract_Col(s,x)
col=s.state(x);
r(:,x)=s.c2(:,col);
end

채택된 답변

per isakson
per isakson 2017년 11월 30일
편집: per isakson 2017년 11월 30일
Your text make me think you want
r = s.c2(:,s.state)
or possibly
r = num2cell( s.c2(:,s.state) )
but your code does something different
  댓글 수: 1
balandong
balandong 2017년 11월 30일
Hi Per, thanks for the quick reply, yes direct indexing is rather straight forward. Really appreciate the time taken to entertain this problem.

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2017년 11월 30일
Why not simply use direct indexing?
r = s.c2(:, s.state)
  댓글 수: 1
balandong
balandong 2017년 11월 30일
Hi Jos, Thanks for the quick reply. Yes, I should use direct indexing. Btw, I had to accept Per answer, since he give the 1st answer. Really appreciate your help

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by