"reshape" does not give column vector entries in output

Running test_modified.m we obtain the vector entries for the residuals , as a 1x8 vector. However, with either
r = reshape([result{:}].',[],1) % column vector
r = reshape([result{:}].',1,[]) % row vector
I obtain no vector output. It says "Brace indexing is not supported for variables of this type."
What is wrong and how can I correct this?
Thanks

4 Comments

type test_modified.m
result = zeros(8,1); %experiment 1 y1=[2;1]; dt=0.01; T=1; k=[5;1]; enzyme(y0,k,dt,T) [SP,grad]=enzyme(y0,k,dt,T); results(1:2) = y1-SP; %experiment 2 y1=[1;1]; dt=0.01; T=1; k=[5;1]; enzyme(y0,k,dt,T) [SP,grad]=enzyme(y0,k,dt,T); results(3:4) = y1-SP; %experime 3 y1=[1;0]; dt=0.01; T=1; k=[5;1]; enzyme(y1,k,dt,T) [SP,grad]=enzyme(y0,k,dt,T); results(5:6) = y1-SP; %experiment 4 y1=[4;1]; dt=0.01; T=1; k=[5;1]; enzyme(y0,k,dt,T) [SP,grad]=enzyme(y0,k,dt,T); results(7:8) = y1-SP r = reshape([result{:}].',[],1) % column vector r = reshape([result{:}].',1,[]) % row vector
Based on the code above -
1 - You have preallocated the array as result whereas you are assinging the values to results (extra s at the end). Correct the mis-match.
Also, as the error states, you can not use brace indexing (i.e. indexing via curly brackets - {}) for numeric data type. Only regular indexing is allowed for numeric data types i.e. use parenthesis.
2 - y0 is not defined in test_modified.m.
3 - It will be better to store data (that is varying) in arrays and call enzyme() N number of times, instead of calling it individually every time.
Thanks @Dyuman Joshi, but y0 is defined in enzyme.m which is called in test_modified.m
@Stephen23 good point! In this case, it would be different, and not V = y1(:)-SP(:). With that, I get
V =
3.0146
-1.1146

Sign in to comment.

Answers (1)

Hello @Sergio,
The error message "Brace indexing is not supported for variables of this type" usually indicates that cell array indexing is being applied to a variable that is not a cell array.
It seems that y0 and y1 are intended to represent concentrations. Since y0 is not defined but is received in the enzyme.m file, I assume you want to pass the defined y1 to the enzyme function as y0. For clarity and simplicity, consider replacing y1 with y0.
The error arises because result is a numeric array, so you should use parentheses () for indexing instead of braces {}.
After making the necessary corrections, your code should look like this:
result = zeros(8,1); % Initialize the correct variable
%Experiment 1
y0 = [2;1]; % Initial condition
dt = 0.01;
T = 1;
k = [5;1];
[SP, ~] = enzyme(y0, k, dt, T); % Capture only necessary outputs
result(1:2) = y0 - SP;
You can apply similar changes for the remaining experiments. To reshape result into a row or column vector, you can use:
r_column = reshape(result, [], 1); % Column vector
r_row = reshape(result, 1, []); % Row vector
Let me know if this helps.

Products

Release

R2023b

Asked:

on 20 Mar 2024

Edited:

on 27 Sep 2024

Community Treasure Hunt

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

Start Hunting!