splitting a vector into separate vectors

6 Ansichten (letzte 30 Tage)
Neda
Neda am 30 Sep. 2024
Bearbeitet: Stephen23 am 30 Sep. 2024
Hi Matlab community,
I have a vector like
V = [2 2 2 2 2 4 4 4 7 7 8 9]
I want to separate this vector into
V1 = [2 2 2 2 2 ], V2=[4 4 4 ], V3=[7 7 ], V4=8, V5=9
Suppose that I do not know the value in vector V. In each iteration, I have a new vector.
Thanks

Akzeptierte Antwort

Stephen23
Stephen23 am 30 Sep. 2024
Bearbeitet: Stephen23 am 30 Sep. 2024
A much better approach using a cell array:
V = [2,2,2,2,2,4,4,4,7,7,8,9]
V = 1×12
2 2 2 2 2 4 4 4 7 7 8 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
L = diff(find([1,diff(V),1]))
L = 1×5
5 3 2 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = mat2cell(V,1,L)
C = 1x5 cell array
{[2 2 2 2 2]} {[4 4 4]} {[7 7]} {[8]} {[9]}
  3 Kommentare
Umeshraja
Umeshraja am 30 Sep. 2024
Hi @Neda,
You can use indexing to extract the individual arrays and then use functions like 'length' or 'numel' to determine the number of elements in each array.
firstArray = C{1};
numElementsInFirstArray = numel(firstArray);
Please refer to the following documentation to know more
Stephen23
Stephen23 am 30 Sep. 2024
Bearbeitet: Stephen23 am 30 Sep. 2024
"Now, how can I access the resutl? I mean how can I understand how many elements are there in the first array? and second array? I mean, how many 2 we have here?"
My answer already tells you that, take a look at the variable L, it gives you the lengths of each vector.
You can access the arrays using very basic MATLAB indexing, eg. the 2nd vector:
C{2} % the vector
L(2) % its length
You do not need to call an extra function like LENGTH or NUMEL, because the information is already there.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Steven Lord
Steven Lord am 30 Sep. 2024
Can you dynamically create variables with numbered names like V1, V2, V3, etc.? Yes.
Should you do this? The general consensus is no. That Discussions post explains why this is generally discouraged and offers several alternative approaches.
  2 Kommentare
Neda
Neda am 30 Sep. 2024
Thank you Steven,
Actually, my problem is not the name, splitting the matrix is my problem.
Neda
Neda am 30 Sep. 2024
Thank you so much Steven. Very helpful !!!

Melden Sie sich an, um zu kommentieren.


Umeshraja
Umeshraja am 30 Sep. 2024
Hi @Neda,
If you are working with large 1D vectors. using a binary search might be a good option to optimize time complexity.
Below is the sample code:
function separatedVectors = separateUsingBinarySearch(V)
% Initialize an empty cell array to store the separated vectors
separatedVectors = {};
n = length(V);
if n == 0
return;
end
startIdx = 1;
% Continue until the start index exceeds the length of the vector
while startIdx <= n
value = V(startIdx);
endIdx = findEndIndex(V, startIdx, n, value);
% Store the current group of identical elements in the cell array
separatedVectors{end+1} = V(startIdx:endIdx);
startIdx = endIdx + 1;
end
end
function endIdx = findEndIndex(V, startIdx, n, value)
low = startIdx;
high = n;
% Perform binary search to find the last occurrence of 'value'
while low < high
mid = floor((low + high + 1) / 2);
if V(mid) == value
low = mid; % Move the lower bound up if the mid value matches
else
high = mid - 1; % Move the upper bound down if the mid value does not match
end
end
endIdx = low;
end
% Example usage
V = [2 2 2 2 2 4 4 4 7 7 8 9 9 9 9];
separatedVectors = separateUsingBinarySearch(V);
% Display the results
for i = 1:length(separatedVectors)
fprintf('[%s]\n', num2str(separatedVectors{i}));
end
[2 2 2 2 2] [4 4 4] [7 7] [8] [9 9 9 9]
The function separateUsingBinarySearch efficiently divides a sorted vector V into sub-vectors, each containing consecutive identical elements. For each starting index, findEndIndex uses binary search to find the last occurrence of the current value efficiently. The sub-vector from startIdx to endIdx is extracted and added to separatedVectors. The process repeats by updating startIdx to just after endIdx for the next group.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by