Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Editing sections of an array that are pointed to by two vectors: one with starting indexes, another with end indexes

1 回表示 (過去 30 日間)
Ben Anstrom
Ben Anstrom 2019 年 12 月 25 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello everyone,
I am trying to access sections of an array such as,
data = [5 7 10 10.5 11.5 13 15 16 20.5 24 30];
to create an equal length logical array based off a logical function i wrote.
The logical function should only be applied between the start and end indexes that are saved in the following variables:
indStart = [0 0 1 0 0 0 0 1 0 0 0];
indEnd = [0 0 0 0 1 0 0 0 0 1 0];
alternatively, after running find() i could use:
inStart = [3 7];
indEnd = [5 10];
I have used arrayfun() to get the following output from the written logical function:
result = arrayfun(@(x1,x2)fun(x1, x2) , indStart, indEnd, 'uni', false);
result = { [0 1 0] }
{ [0 1 1 0] }
The problem that i am having is that i want to have this data superimposed onto an array of zeros that has a an equal length to data. Such as:
result = [0 0 0 1 0 0 0 1 1 0 0];
I know i can do this quite easily with a for loop, but i am trying to accomplish this using indexing and logical statements so that processing time stays down. The final script needs to be processed by ga(), so any cut down on processing time is very valuable.
Thank you to anyone who can help!
  2 件のコメント
David Hill
David Hill 2019 年 12 月 25 日
My understanding is that arrayfun() is the same as a for-loop. What is the logical function you are applying?
dpb
dpb 2019 年 12 月 25 日
David is quite right-- arrayfun is probably going to be slower than a straightforward loop altho it does have it's place.
I'm also having a heckuva time trying to figure out just what it is that you're trying to do, specifically.
Not having the function definition doesn't help...
I think you've tried and maybe I'm just dense but I'd suggest outline a sample input array and then the desired output and how know that's the right input.
It would seem that perhaps combining the two logical position vectors filling in the runs might solve the problem...

回答 (1 件)

per isakson
per isakson 2019 年 12 月 25 日
編集済み: per isakson 2019 年 12 月 25 日
Try
>> isSelected = logical([cumsum(indStart),0]-[0,cumsum(indEnd)]);
>> isSelected(end) = [];
>> data(isSelected)
ans =
10 10.5 11.5 16 20.5 24
where indStart and indEnd are the logical variables

Community Treasure Hunt

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

Start Hunting!

Translated by