Extraction of elements of an array inside the other array

Suppose i have
period(t1,t2,t3) =time(p)
where t1, t2, t3 are n dimensional arrays say
t1 = (1,3,4,6,8,9,0,5,4)
t2 = (3)
t3 = (4,56,7,8,5,1)
i want to show the 5th element of t1 as the output.
Output should be 8
how do i do that ? Kindly help me :)

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 4 月 4 日

0 投票

period(t1,t2,t3) =time(p)
So t1, t2, t3 are subscripts, and time(p) is either a function or array that results in either a scalar or else a length(t1) by length(t2) by length(t3) array. Afterwards, period will be max(t1) by max(t2) by max(t3) in size.
t1 = (1,3,4,6,8,9,0,5,4,)
0 is not a valid index, so t1 cannot be used as a subscript for an array.
For the sake of continuing the discussion, let us instead say
t1 = [1,3,4,6,8,9,10,5,4]
t2 = [3]
t3 = [4,56,7,8,5,1]
and now we know that whatever time(p) is, it must be either a scalar or else a 9 x 1 x 6 array
i want to show the 5th element of t1 as the output.
t1(5)
but what does that have to do with "inside another array" ??

9 件のコメント

Ganesh Kini
Ganesh Kini 2020 年 4 月 4 日
I will try to explain this way
Period (5,1,3) = 12
so the value of time is 12
Now i have trace back by taking the value of time i.e. 12, i need to get the value of the 5th element of t1
is it possible ?
Ganesh Kini
Ganesh Kini 2020 年 4 月 5 日
Can someone help me out ?
Ganesh Kini
Ganesh Kini 2020 年 4 月 5 日
編集済み: Walter Roberson 2020 年 4 月 6 日
a = (1,3,4,6,8,9,0,5,4,)
b = (3)
c = (4,56,7,8,5,1)
a, b,c are saved in .vec extension
for t1 =1:1:length(a)
for t2=1:1:length(b)
for t3=1:1:length(c)
period_fun(a,b,c)=time(p);
p=p+1;
end
end
end
So this code will give me the time
period fun(5,1,3) which means it will fetch
5th element of a = 8, 1st element of b = 3, 3rd element of c = 7, and the output is time = 1.1
Now just by looking the output time 1.1 i want to extract the 5th element of a or the 1st element of b or 3rd element of c and get that element as output
is it possible ?
Image Analyst
Image Analyst 2020 年 4 月 5 日
Try ismember()
Ganesh Kini
Ganesh Kini 2020 年 4 月 5 日
Thank you, is it possible to give me some more details ?
Walter Roberson
Walter Roberson 2020 年 4 月 5 日
(Sorry for the image of a reply instead of the text of a reply; my session froze but I was able to rescue a picture of it.)
Walter Roberson
Walter Roberson 2020 年 4 月 6 日
What I suspect you are looking for:
for t1 =1:1:length(a)
for t2=1:1:length(b)
for t3=1:1:length(c)
period_fun(t1,t2,t3)=time(p);
p=p+1;
end
end
end
if time is a vector or array of values then you can instead use:
na = length(a);
nb = length(b);
nc = length(c);
period_fun = permute( reshape(time(1:na*nb*nc), nc, nb, na), [3 2 1] );
with no loop.
After that I suspect you want:
target = 1.1;
[found, idx] = ismembertol(target, period_fun);
[aidx, bidx, cidx] = ind2sub([na,nb,nc], idx);
corresponding_a = a(aidx).';
corresponding_b = b(bidx).';
corresponding_c = c(cidx).';
Each of those could be a vector if there is more than one entry in target that is close enough to the target value.
Ganesh Kini
Ganesh Kini 2020 年 4 月 6 日
Thank you.
If you have spare time, could you please explain me the above code ?
Ganesh Kini
Ganesh Kini 2020 年 4 月 7 日
Hi,
after looking at the existing code and analysis.
period_fun(a,b,c)=time(p);
period_fun is not a function, its just an array.
Could you please let me know how to access the elements passed to it ?

この質問は閉じられています。

タグ

質問済み:

2020 年 4 月 4 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by