how can I take elements of an array as input to a function in a set of 10 elements each?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have an array of [1x676].
I need to input this array [1x676] in a function through testbench. how can I input it in a set of 26 elements each.?
this is part of a code that is needed for hdlcoder. so I have to reduce number of IOs
0 Kommentare
Antworten (3)
John D'Errico
am 21 Jun. 2023
Bearbeitet: John D'Errico
am 21 Jun. 2023
A terribly confusing question. (Even if I ignore the fact that your subject says 10 elements, but the question text says 26 elements each.) Do you merely want to pass in blocks of 26 elements, one block at a time into the function?
If so, then just use a loop. People get too hung up on needing to have everything neatly vectorized, so it works in one line as if by magic. At the same time, that one line becomes impossible to read, debug and maintain, and runs no more quickly than a simple loop. And this would not be even a big loop.
If you really insist on something that makes even the above loop more easy, then first reshape the array, into a 26x26 array.
help reshape
Then, taking each column of that array as a vector, pass it into your function, one at a time. Seriously, there is nothing sophisticated needed here.
Or, do you want some magical piece of code that will generate all possible subsets of 26 elements? If so, then you need to wait for a new computer, possibly waiting many years, since the number of ways you can extract subsets of size 26 from 676 elements is literally too large to even comprehend.
nchoosek(676,26)
So, roughly the square of Avogadro's number. HUGE. IMMENSE.
Or maybe you are wanting somethign else. Perhaps some code that will choose an optimal subset from that large set. In that case, you need to explain what it is you really need to do.
I'm betting that you just need to use a simple loop.
0 Kommentare
Mudit Kumar Bhugari
am 21 Jun. 2023
As per my knowledge, you have an array of size [1x676] and you want to input it into a function in sets of 26 elements each. You can divide the array into smaller subsets of size 26 and pass them to the function one by one.
Here's a sample code which can give you a better understanding
arr = 1:676;
% Divide the array into subsets of size 26
setSize = 26;
numSets = numel(array) / setSize;
for i = 1:numSets
stIdx = (i - 1) * setSize + 1;
endIdx = stIndex + setSize - 1;
subset = array(startIndex:endIdx);
% Call your function with the current subset as input
output = function_name(subset);
end
0 Kommentare
Lakshay Rose
am 21 Jun. 2023
Hi Jyoti Nautiyal,
As per my understanding you are trying to divide your array of 1X676 into sets of 26 each and then input them to a function.
To get the following result you can try the below code –
array = 1:676; % Example array [1x676]
numSets = ceil(length(array) / 26); % Calculate the number of sets
for i = 1:numSets
startIndex = (i - 1) * 26 + 1; % Calculate the start index of the current set
endIndex = min(startIndex + 26 - 1, length(array)); % Calculate the end index of the current set
currentSet = array(startIndex:endIndex); % Extract the current set of 26 elements
% Call your function with the current set as an input
yourFunction(currentSet);
end
You can replace the `yourFunction` with the function which you want to call.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!