I don't know how to avoid eval

Hi friends from community,
I am very confused on how to manage 'eval' function existing in my code, i need it to be removed from my code and run the function as fast as possible.
The subIndex function runs as to solve such a problem.
Given an integer N, I want the summation of these M non-negative integers be less or equal than N.
List all cases of , such that .
For example
N = 2, M = 2;
Then p becomes
p = [0,0; 1,0; 0,1; 2,0; 0,2; 1,1];
Following is the function with 'eval':
function p = subIndex(M,N)
s = (N+1)*ones(1,M);
r = (1:prod(s))';
ch = '[';
for i = 1:1:M
ch = [ch,'f',num2str(i),','];
end
eval([ch(1:end-1),']=ind2sub(s,r);']);
p = eval([ch(1:end-1),']-1']);
q = sum(p,2);
[~,idx] = sort(q);
p = p(idx,:);
q = sum(p,2)<=N&sum(p,2)>=1;
p = p(q,:);
end

7 comentarios

Wan Ji
Wan Ji el 3 de Sept. de 2021
@Stephen @Jan I need your help.
Stephen23
Stephen23 el 4 de Sept. de 2021
@Wan Ji: the simple and efficient MATLAB approach is to use comma-separated lists (just as Robert U showed):
When you start to mess around with evaluating text then you are doing something wrong.
Chunru
Chunru el 4 de Sept. de 2021
Could it be a feature request to Mathworks such that ind2sub will return an array of sub when there is only one output argument? The solution of "[f{:}]=..." is good. But returning an array is better.
A = sub2ind([5 1], [2 4])
A = 1×2
2 4
So a single output is possible and meaningful... but probably not at all common.
So instead of returning a cell when only one output is requested, it would probably make more sense to add an option, such as 'OutputFormat', 'cell'
In the above example, the code is for sub->ind. The syntax should be IND = sub2ind(SIZ,I,J).
A = sub2ind([5 7], [2 4], [3 2])
A = 1×2
12 9
What we are looking for should be ind->sub:
A = ind2sub([2,4], 1:6)
A = 1×6
1 2 3 4 5 6
If the number of output argument is 1, it just return the linear index. It should be more meaningful to return an arrray of subscripts. However, there might be side effects when the second input argumet (linear index) is an array.
[A, B] = ind2sub([2,4], 1:6)
A = 1×6
1 2 1 2 1 2
B = 1×6
1 1 2 2 3 3
Stephen23
Stephen23 el 4 de Sept. de 2021
Editada: Stephen23 el 4 de Sept. de 2021
@Chunru: this is easy to implement, if you really need this feature.
If you take a copy of the function you can see that the output uses VARARGOUT.
Chunru
Chunru el 4 de Sept. de 2021
@Stephen Sure it is not difficult to implement it. But we have to maintain the code ourselves that is very much similar to a built-in code.

Iniciar sesión para comentar.

 Respuesta aceptada

Robert U
Robert U el 3 de Sept. de 2021
Hi Wan Ji,
you can use cell-arrays. You don't need to assign variables with different names to multiple outputs.
function p = subIndex(M,N)
% Given an integer N, I want the summation of these M non-negative integers be less or equal than N.
% List all cases of p1,p2,...pM, such that sum(p1...pM) <= N.
s = (N+1)*ones(1,M);
r = (1:prod(s))';
f = cell(1,M);
[f{:}] = ind2sub(s,r);
f = [f{:}];
p = f-1;
q = sum(p,2);
[~,idx] = sort(q);
p = p(idx,:);
q = sum(p,2)<=N&sum(p,2)>=1;
p = p(q,:);
end
Kind regards,
Robert

Más respuestas (2)

Walter Roberson
Walter Roberson el 3 de Sept. de 2021
[f{1:M-1}] = ind2sub(s, r);
p = cell2mat(f)-1;
However, it looks to me as if you are doing "integer partitions" of M. People have posted functions for that, including https://www.mathworks.com/matlabcentral/answers/226437-obtain-all-integer-partitions-for-a-given-integer#answer_497158

2 comentarios

Robert U
Robert U el 3 de Sept. de 2021
Hi Walter,
I am not convinced that "M-1" is correct. For the case M = 2 you would get only one cell with the x values of ind2sub. You would miss the second column of p if I am not mistaken.
Kind regards,
Robert
Ah yes I was confused about what the -1 was doing in the original code. I see now that it was skipping a final comma. eval() is such a mess to use!
Also... strjoin() would have avoided having to remove the comma. And the loop could have been avoided with
strjoin("f"+(1:M), ',')

Iniciar sesión para comentar.

Chunru
Chunru el 3 de Sept. de 2021
N = 2, M = 2;
%p = [0,0; 1,0; 0,1; 2,0; 0,2; 1,1];
p = subIndex(M,N)
p = subIndex1(M,N)
function p = subIndex(M,N)
s = (N+1)*ones(1,M);
r = (1:prod(s))';
ch = '[';
for i = 1:1:M
ch = [ch,'f',num2str(i),','];
end
eval([ch(1:end-1),']=ind2sub(s,r);']);
p = eval([ch(1:end-1),']-1']);
q = sum(p,2);
[~,idx] = sort(q);
p = p(idx,:);
q = sum(p,2)<=N&sum(p,2)>=1;
p = p(q,:);
end
function p = subIndex1(M,N)
% Consider the M numbers are index pf M-D matrix, each dim has N+1 elements
s = (N+1)*ones(1,M);
k = cumprod(s);
ii = (0:k(end)-1)'; % linear index (0 based)
idx = zeros(length(ii), M); % sub []xM
% linear index i--> sub idx of size Mx1 (0 based)
for j=M:-1:2
ir = rem(ii, k(j-1));
idx(:, j) = (ii - ir) /k(j-1);
ii = ir;
end
idx(:, 1) = ii;
p = idx(sum(idx,2)<=N, :);
end

Categorías

Productos

Versión

R2020a

Preguntada:

el 3 de Sept. de 2021

Comentada:

el 4 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by