Saving in 3d matrix Input and Output results from loop
Show older comments
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)
Star Strider
on 2 May 2015
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
Star Strider
on 3 May 2015
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!
Star Strider
on 3 May 2015
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
Star Strider
on 3 May 2015
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.
Ac
on 3 May 2015
Star Strider
on 3 May 2015
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.
Ac
on 4 May 2015
Star Strider
on 4 May 2015
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!
Ac
on 5 May 2015
Star Strider
on 5 May 2015
My pleasure.
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!