0

I would like to create parametrized matrix with given relationship between arguments like this:

B= @(A) [1*A(1), 2*A(2), 3*A(3), 4*A(4)];

But matrix is huge and cannot be created by constant expression like this. I need something like this:

for i=1:N
    B(i) = @(A) i*A(i);
end

Creating matrix this way is not possible and the cell array did not helped me, because this (bellow) is also not valid.

B = @(A) cell(1,N);
for i=vec
    B(i) = @(A) i*A(i);
end
1
  • B = @(A) [1:numel(A)].*A? Commented Jul 23, 2023 at 14:43

1 Answer 1

0

What about creating a function?

function B = create_matrix(A,n)
    B = zeros(1,n);
    for ii=1:n
        B(ii) = ii*A(i);
    end
end
Sign up to request clarification or add additional context in comments.

5 Comments

This is what I have now. But if I put B = create_matrix(A,N), then the matrix is probably allocated newly. I want to speed it up with knowledge of constant dimensions and relationships of parameters inside matrix B - so constant dimensions and variable values due to inserted parameter A.
please, add a minimal reproducible example of what you want. expected input and output and why this answer does not help you and what you have tried to do
In C langugage, allocation of memory would be something like below and then C[i] will contain pointers to members of A and B. If A and B changes, then C changes. const double* C = (double*) calloc(N, sizeof(double));
@Ales100 That is what B = zeros(1,n); does: it allocates an array of N doubles and initializes it to zeros.
@beaker but it allocates it every function call -> every update. What I have shown allocates memory only once and then are changes solved by links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.