Similar code as vec2ind function for MATLAB coder?

6 vues (au cours des 30 derniers jours)
HASAN AL-KAF
HASAN AL-KAF le 16 Oct 2021
Réponse apportée : Sathvik le 11 Nov 2024 à 9:53
Hi
I want a code that give similar function as vec2ind.
I found this code sum( repmat((1:size(vec,1)).', 1, size(vec,2)) .* vec)
but this code doesn't give same result as vec2ind
for example vec= [1;0;1;0;1;1;5;0]
The result of vec2ind is 7.
The result of sum( repmat((1:size(vec,1)).', 1, size(vec,2)) .* vec) is 50.
How can get exactly same result?. I want to transfer This matlab code to c++.
Thank you.

Réponses (1)

Sathvik
Sathvik le 11 Nov 2024 à 9:53
Hi,
To use a similar function as vec2ind, you can use the following code which supports code generation:
vec = [1;0;1;0;1;1;5;0];
wasMatrix = ~iscell(vec);
x = {vec};
y = cell(size(x));
for i=1:numel(x)
[~,y{i}] = max(x{i},[],1);
end
if wasMatrix
y = y{1};
end

Community Treasure Hunt

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

Start Hunting!

Translated by