How do I reduce this nxmx3 matrix to a (n*m)x3 matrix?

2 visualizzazioni (ultimi 30 giorni)
I am running simulations in which I solve a system of 3 equations and 3 unknowns for n values of a parameter, with m random guesses in the solver for each of the n parameter values. The solutions array is nxmx3. I want to separate the solutions along the second dimension into m nx3 matrices and stack them into a 2 dimensional matrix (n*m)x3 where I am effectively displaying all possible solutions, sorted by the parameter value. Here are the 3 equations:
if true
function F=equations(a,S) %S is the parameter I am varying along the first dimension
F(1)=2*((a(3))^2-(a(1))^2 )^0.5-((1-2*a(1)))/((1-2*a(1))^2+...
(1-2*a(2))^2 )^0.5*((a(3))^2-0.25*((1-2*a(1))^2+(1-2*a(2))^2 ))^0.5;
F(2)=2*((a(3))^2-(a(2))^2 )^0.5-((1-2*a(2)))/((1-2*a(1))^2+...
(1-2*a(2))^2 )^0.5*((a(3))^2-0.25*((1-2*a(1))^2+(1-2*a(2))^2 ))^0.5;
F(3)=acos(0.5*((1-2*a(1))^2+(1-2*a(2))^2)^0.5/a(3))+...
acos((a(1))/(a(3)))+acos((a(2))/(a(3)))+(0.5*((1-2*a(1))^2+...
(1-2*a(2))^2)^0.5*((a(3))^2-0.25*((1-2*a(1))^2+(1-2*a(2))^2 ))^0.5)...
/((S-2*(a(3))^2))+(a(1)*((a(3))^2-(a(1))^2 )^0.5)/((S-2*(a(3))^2))...
+(a(2)*((a(3))^2-(a(2))^2)^0.5)/((S-2*(a(3))^2))-pi;
end
end
Here is my code for solving the system.
if true
clear;
clc;
%Preallocations
n=10;% This tells me how many values of S I use. I made it small for your convenience.
m=5;% This tells me how many guesses are guessed per S. I made it small for your convenience.
draw=100;
S=zeros(n,1);
guess=zeros(n,m,3);
b=zeros(n,m,1);
c=zeros(n,m,1);
d=zeros(n,m,1);
solutions=zeros(n,m,3);
for t=1:n;
S(t)=(t+n/(3+2^1.5))/n; %this is the parameter that varies. n values.
for k=1:m;
%generating the guess matrix
b(t,k)=randsample(draw,1);
c(t,k)=randsample(draw,1);
d(t,k)=randsample(draw,1);
guess(t,k,1)=b(t)/(2*draw+1);
guess(t,k,2)=c(t)/(2*draw+1);
guess(t,k,3)=d(t)*1.2/(2*draw+1);
%solving the off-diagonal system
system = @(a)equations(a,S(t));
solutions(t,k,:)=fsolve(system,guess(t,k,:));%=[a(1) a(2) a(3)]
end
end
end
Now that I have the nxmx3 solutions matrix, I want to remove the second dimension and somehow "stack" the m nx3 matrices into one big (n*m)x3 matrix. I tried using a for loop and cat() but I don't think that is a good way. Here is how I started. Please advise. Your help is GREATLY appreciated!
if true
sols2d=zeros(m*n,3);
for k=1:m
submat(k)=solutions(:,k,:);%dimension mismatch error here
rsubmat(k)=reshape(submat(k),[n,3]); %tried to make m nx3 matrices
cat(1,[]);
end
end
Thank you!

Risposta accettata

Chris Turnes
Chris Turnes il 5 Gen 2016
It sounds like you want a combination of permute and reshape. Here is an example:
>> m = 3;
>> n = 4;
>> A = reshape(1:(m*n*3), [m n 3]); % make an example array
>> B = reshape(permute(A, [2 1 3]), [m*n 3]);
>> % First nx3 solution:
>> B(1:n,:)
ans =
1 13 25
4 16 28
7 19 31
10 22 34
>> % Compare against original entries in A:
>> A(1, :, :)
ans(:,:,1) =
1 4 7 10
ans(:,:,2) =
13 16 19 22
ans(:,:,3) =
25 28 31 34
  1 Commento
Matt
Matt il 5 Gen 2016
Thank you for the help! I couldn't get what you wrote to work exactly how I wanted it, but this helped me a lot to get there. It turns out it was even simpler than that!
twoD_solutions=reshape(solutions,(n*m),3);
Thanks!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by