How to take the middle of a list?

So I am trying to find an efficient way to take the middle numbers of a list so I can match it to another list.
For example:
a = [1 2 3 4 5 6 7 ];
b = [ 8 9 10 ];
I would want to reduce to a = [ 3 4 5 ] so it has the same ammount of values as b. The ideas I have came up with are really specific and don't work well when numbers are changed. So if anyone has any advice it would be greatly appreciated!
Thanks,
William

Antworten (2)

John D'Errico
John D'Errico am 15 Jun. 2023

1 Stimme

"really specific" issues probably means that it makes no sense to ask for the middle 3 elements of a VECTOR (Python has lists, MATLAB just has vectors) when the vector has an even length.
That is, what are the middle 3 elements of the vector V?
V = 1:6
V = 1×6
1 2 3 4 5 6
Likewise, what are the middle 4 elements of the vector 1:5?
As long as the parity of vector lengths is the same, then it is pretty easy.
middle = @(V,m) V(((numel(V) - m)/2 + 1):((numel(V) + m)/2));
Now it is easy to do, as long as the length of V and m have the same parity, thus both are either even or odd.
middle(1:6,2)
ans = 1×2
3 4
middle(1:6,4)
ans = 1×4
2 3 4 5
Odd parities for both also work.
middle(1:7,1)
ans = 4
middle(1:7,3)
ans = 1×3
3 4 5
middle(1:7,5)
ans = 1×5
2 3 4 5 6
And it complains when there are mixed parities.
middle(1:3,2)
Warning: Integer operands are required for colon operator when used as index.
ans = 1×2
2 3
A well written function would be smarter, and test those parities in advance, and then bounce out with an error message. I'll let you do that part.

3 Kommentare

William
William am 15 Jun. 2023
This calrifies a lot of questions that I had. Thank you very much, John. I am all self taught so sometimes I get confused with lists/vectors and stuff like that. Appreciate it a lot
John D'Errico
John D'Errico am 15 Jun. 2023
Actually, I suppose MATLAB does have the concept of a comma separated list, but it is not really anything you use very often.
William
William am 15 Jun. 2023
That's kinda what I had thought

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 15 Jun. 2023

0 Stimmen

Try this:
a = [1 2 3 4 5 6 7 8 9 10 1 2 3 8 9 10 88 83 99];
b = [ 8 9 10 ];
% Find starting indexes where b is in a:
indexes = strfind(a, b)
indexes = 1×2
8 14

3 Kommentare

William
William am 15 Jun. 2023
So this just returns where there is an 8 in a. Not exactly what I am looking for
Image Analyst
Image Analyst am 15 Jun. 2023
Well we don't really know what you meant since you didn't explicitly describe when you said you wanted to take a few elements (a sub-vector) and "match it to another list". What does that mean exactly? I thought it meant that you wanted to find where the values in one small vector occurred/matched those in another longer vector. I guess not. Sorry I misinterpreted what you want. If you still need help, explain a lot better what you want.
Dyuman Joshi
Dyuman Joshi am 17 Jun. 2023
@William You have flagged @Image Analyst's comment saying "It was just really rude"?
Care to explain where exactly was Image Analyst was rude in their comment?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Gefragt:

am 15 Jun. 2023

Kommentiert:

am 17 Jun. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by