필터 지우기
필터 지우기

Save array in matrix from for loop

조회 수: 2 (최근 30일)
Karl Zammit
Karl Zammit 2021년 12월 11일
댓글: Karl Zammit 2021년 12월 12일
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

채택된 답변

KSSV
KSSV 2021년 12월 11일
편집: KSSV 2021년 12월 11일
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
Karl Zammit
Karl Zammit 2021년 12월 12일
The first method worked just fine thanks. The second method no.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by