Save array in matrix from for loop

6 views (last 30 days)
Karl Zammit
Karl Zammit on 11 Dec 2021
Commented: Karl Zammit on 12 Dec 2021
I am trying to save two arrays (called pstar A and pstarB) in matrix form such that I can than plot each value as a point against the value of x, however, I am being presented with the error "Unable to perform assignment because the left and right sides have a different number of elements."
Any clues on how to do so and how to obtain the plot thereafter?
for x=xo:((xf-xo)/divnum):xf
% radial coordinate
rA = x*bA;
rB = x*bB;
% pressure due to rotation and radial variability
pabsA = (rho*(swirl^2)*(omegaA^2)*((rA^2)-(r0A^2))/2)-((rho*(avolflowA.^2))/(8*(pi^2)*(sA^2)*((rA^2)-(r0A^2))));
pabsB = (rho*(swirl^2)*(omegaB^2)*((rB^2)-(r0B^2))/2)-((rho*(avolflowB.^2))/(8*(pi^2)*(sB^2)*((rB^2)-(r0B^2))));
% pstar
pstarA = pabsA / (rho*(omegaA^2)*(bA^2));
pstarB = pabsB / (rho*(omegaB^2)*(bB^2));
psA = [pstarA, psA+1];
psB = [pstarB, psB+1];
counter = counter+1;
end

Accepted Answer

KSSV
KSSV on 11 Dec 2021
Edited: KSSV on 11 Dec 2021
X=xo:((xf-xo)/divnum):xf
N = length(X) ;
pstarA = cell(N,1) ;
pstarB = cell(N,1) ;
for i = 1:N
x = X(i) ;
% radial coordinate
rA = x*bA;
rB = x*bB;
% pressure due to rotation and radial variability
pabsA = (rho*(swirl^2)*(omegaA^2)*((rA^2)-(r0A^2))/2)-((rho*(avolflowA.^2))/(8*(pi^2)*(sA^2)*((rA^2)-(r0A^2))));
pabsB = (rho*(swirl^2)*(omegaB^2)*((rB^2)-(r0B^2))/2)-((rho*(avolflowB.^2))/(8*(pi^2)*(sB^2)*((rB^2)-(r0B^2))));
% pstar
pstarA{i} = pabsA / (rho*(omegaA^2)*(bA^2));
pstarB{i} = pabsB / (rho*(omegaB^2)*(bB^2));
end
Also try:
X=xo:((xf-xo)/divnum):xf
N = length(X) ;
pstarA = zeros(N,1) ;
pstarB = zeros(N,1) ;
for i = 1:N
x = X(i) ;
% radial coordinate
rA = x*bA;
rB = x*bB;
% pressure due to rotation and radial variability
pabsA = (rho*(swirl^2)*(omegaA^2)*((rA^2)-(r0A^2))/2)-((rho*(avolflowA.^2))/(8*(pi^2)*(sA^2)*((rA^2)-(r0A^2))));
pabsB = (rho*(swirl^2)*(omegaB^2)*((rB^2)-(r0B^2))/2)-((rho*(avolflowB.^2))/(8*(pi^2)*(sB^2)*((rB^2)-(r0B^2))));
% pstar
pstarA(i) = pabsA / (rho*(omegaA^2)*(bA^2));
pstarB(i) = pabsB / (rho*(omegaB^2)*(bB^2));
end
  1 Comment
Karl Zammit
Karl Zammit on 12 Dec 2021
The first method worked just fine thanks. The second method no.

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!