Saving in 3d matrix Input and Output results from loop

Dear all,
I'm quite new to matlab and having problems saving input and output data from a loop. What I'm doing is to run a function changing its three main parameters one at the time, A, B and C, this way:
if true
for A = 1:50;
for B = 1:0.05:200;
for C = 1:300;
[R] = myfunction (A,B,C)
end
end
end
end
The complete code works fine, but I need to save in a 3d matrix the output R AND each value of A, B and C for the corresponding output. Can you help me with an example?
Thanks in advance!
A

Answers (1)

A cell array is the best option:
k1 = 0;
for A = 1:50;
for B = 1:0.05:200;
for C = 1:300;
k1 = k1 + 1;
[R] = myfunction (A,B,C)
RABC{k1} = {R, A, B, C};
end
end
end
I arbitrarily called the cell array ‘RABC’. It uses a simple counter to increment it.

10 Comments

Ac’s ‘Answer’ of nearly 12 hours ago [16:00 UCT] (sorry!) moved here...
Thanks for the quick reply!
I tried and works, but what if I want to plot the data? Because in the future I will want to plot for example my R against only A, or B or a mix of the three parameters. This is why I was thinking about a matrix, so I can access the data as I want.
thanks!
My pleasure!
I apologise for the delay. I got into a quite pointless flame war with someone on a different thread that took much longer than it should have and that temporarily derailed my train of thought.
Add these lines after the loop to illustrate how to get the data out of the cell array. This is for cell #1, but it would work for all other cells, possibly in an array. (Plotting an array against a scalar piques my interest, so let me know how that goes.)
Again, my apologies for the delay. I usually do my best to monitor things so there’s usually only a few minutes delay (unless I’m asleep). I don’t have that excuse this time.
The lines:
Q1 = RABC{1}(1) % Retrieve Matrix
Q2 = RABC{1}(2) % Retrieve A
Q3 = RABC{1}(3) % Retrieve B
Q4 = RABC{1}(4) % Retrieve C
Ac
Ac on 3 May 2015
Edited: Ac on 3 May 2015
No worries at all for the delay and thanks again for the help.
I added the lines, and tested. Now yes, it saves only the first value of A, B and C.. but when I try to plot I have the error
Not enough input arguments.
I also tried to extract all the cells, because what I will need in the end the entire values of results and inputs to plot all together, but without success...
UPDATE
I tried to access the data this way,
tmp = vertcat(RABC{:}); DATA = cell2mat(tmp);
And seems to work..
I can’t figure out how you would plot your matrix as a function of the other three variables, so I am at a loss as to what the problem might have been. If ‘R’ is a 3D matrix as you stated, the ‘tmp’ assignment would give an (Nx4) cell of the elements of ‘RABC’, and the cell2mat call would crash because the matrix and the three scalars are not compatible.
There’s obviously something about your problem that isn’t in your Question.
No wait, my question was how to save in a 3d matrix the output R and A,B,C.
R is not a 3D matrix, but just a number, the output of my function.
So, in my original question I was thinking about a 3D matrix because the idea is: - first in my loop I calculate R based on A,B and C and save everything. - Then, I can pick from the matrix the data I want to plot and see which ones are the most useful ones. I hope I have clarified!
In your question, you implied that the output of your function, ‘R’, was a 3D matrix. At least that’s how I read it, since ‘A’, ‘B’ and ‘C’ are obviously scalars.
A 3D matrix is something like an image (which is (NxMx3)) or any other matrix with several (NxM) ‘pages’, stacked to create a third dimension.
You are now describing a simple four-element vector that you can easily save as:
RABC(k1,:) = [R, A, B, C];
You cannot use ‘B’ as an index, because it has a fractional increment. In MATLAB, index variables must be integers >0. That is the reason I introduced the ‘k1’ counter.
Sorry if my question wasn't clear! The original idea of having a 3D matrix is because in case I need to plot - for example - R in function of A, B but only a range - or a value - of C, I thought was the best way.
If all your values are scalars, this should work:
B = 1:0.05:200;
for A = 1:50;
for Bidx = 1:length(B);
for C = 1:300;
[R] = myfunction (A,B(Bidx),C)
RABC(A,Bidx,C) = R;
end
end
end
Note: Untested code!
I tried, but now I am having an error in my tspan of the ode45 solver. I'll try to figure out where is the problem and see if it works.
thanks!

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Asked:

Ac
on 2 May 2015

Commented:

on 5 May 2015

Community Treasure Hunt

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

Start Hunting!