I face the error of "In an assignment A(I) = B, the number of elements in B and I must be the same" when i want to create a loop a matrix. my simple short code given below, plz help me?

1 Ansicht (letzte 30 Tage)
clc clear x1=5; x2=3; x3=2; for i=1:3 if i==1 x1=6; elseif i==2 x1=7; elseif i==3 x1=8; end p(i)=[x1-x2 x3-x2; x2-x3 x1-x3] end

Akzeptierte Antwort

Mahdi
Mahdi am 3 Jun. 2014
p(i) is one element, but you are trying to tell it to store multiple values, one of the following would fix your problem (Just replace it):
p(i,:)=[x1-x2 x3-x2; x2-x3 x1-x3]
or
p(:,i)=[x1-x2 x3-x2; x2-x3 x1-x3]
  2 Kommentare
Mehedi
Mehedi am 4 Jun. 2014
Now i face another error of "Subscripted assignment dimension mismatch." plz, help me out
Mahdi
Mahdi am 4 Jun. 2014
This is a quick non-efficient fix to it.
clc
clear
p=[];
x1=5; x2=3; x3=2;
for i=1:3
if i==1
x1=6;
elseif i==2
x1=7;
elseif i==3
x1=8;
end
p=[p;x1-x2 x3-x2; x2-x3 x1-x3]
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by