-1

I am cheema and want to save values that are in matrix form (in column) to a saved matrix in .dat file.

Actually, I can successfully add new values preserving the previous one's when I have only one scalar value, but want to add up a complete column to the saved matrix preserving the previous values. So that I can plot all the values from start to end. The main theme is described in the code:

for i=1:100
 k=i+5;
 l=i+2;

a=[k l];
b=[k1 l1];
c=abs(a-b);
h=(k+k1)/2;
h1=(h+k1)/2;
h2=(h+k)/2;
h3=(h1+k1)/2;
h4=(h+h1)/2;
h5=(h+h2)/2;
h6=(h2+k)/2;
h7=(h3+k1)/2;
h8=(h1+h3)/2;
h9=(h1+h4)/2;
h10=(h+h4)/2;
h11=(h+h5)/2;
h12=(h5+h2)/2;
h13=(h2+h6)/2;
h14=(h6+k)/2;
g=(l+l1)/2;
g1=(g+l1)/2;
g2=(g+l)/2;
g3=(g1+l1)/2;
g4=(g+g1)/2;
g5=(g+g2)/2;
g6=(g2+l)/2;
g7=(g3+l1)/2;
g8=(g1+g3)/2;
g9=(g1+g4)/2;
g10=(g+g4)/2;
g11=(g+g5)/2;
g12=(g5+g2)/2;
g13=(g2+g6)/2;
g14=(g6+l)/2;

if c>=[0 0] & c<[8 5]
    k2=k;
    l2=l;
elseif c>=[8 0]& c<[12 5]
    k2=[h;k];
    l2=[l;l];
elseif c>=[12 0] & c<[25 5]
    k2=[h1;h;h2;k];
    l2=[l;l;l;l];
elseif c>=[25 0] & c<[50 5]
    k2=[h3;h1;h4;h;h5;h2;h6;k];
    l2=[l;l;l;l;l;l;l;l];
elseif c>=[0 5] & c<[8 10]
    k2=[k;k];
    l2=[g;l];
elseif c>=[0 10] & c<[8 25]
    k2=[k;k;k;k];
    l2=[g1;g;g2;l];
elseif c>=[0 25] & c<[8 50]
    k2=[h;h;h;h;h;h;h;h];
    l2=[g3;g1;g4;g;g5;g2;g6;l];
elseif c>=[8 5] & c<[12 10]
    k2=[h;k];
    l2=[g;l];
elseif c>=[8 10] & c<[12 25]
    k2=[h;h;k;k];
    l2=[g1;g;g2;l];
elseif c>=[8 25] & c<[12 50]
    k2=[h;h;h;h;k;k;k;k];
    l2=[g3;g1;g4;g;g5;g2;g6;l];
elseif c>=[12 5] & c<[25 10]
    k2=[h1;h;h2;k];
    l2=[g;g;l;l];
elseif c>=[12 10] & c<[25 25]
    k2=[h1;h;h2;k];
    l2=[g1;g;g2;l];
elseif c>=[12 25] & c<[25 50]
    k2=[h1;h1;h;h;h2;h2;k;k];
    l2=[g3;g1;g4;g;g5;g2;g6;l];
elseif c>=[25 5] & c<[50 10]
    k2=[h3;h1;h4;h;h5;h2;h6;k];
    l2=[g;g;g;g;l;l;l;l];
elseif c>=[25 10] & c<[50 25]
    k2=[h3;h1;h4;h;h5;h2;h6;k];
    l2=[g1;g1;g;g;g2;g2;l;l];
elseif c>=[25 25] & c<[50 50]
    k2=[h3;h1;h4;h;h5;h2;h6;k];
    l2=[g3;g1;g4;g;g5;g2;g6;l];
else
    k2=[h7;h3;h8;h1;h9;h4;h10;h;h11;h5;h12;h2;h13;h6;h14;k];
    l2=[g7;g3;g8;g1;g9;g4;g10;g;g11;g5;g12;g2;g13;g6;g14;l];
end
q(i,:)=[k2 l2];


 save path.dat q -ascii;


 x=path(:,1);
 y=path(:,2);
plot(x,y,'-o')
 k1=k;
  l1=l;
end

I am getting error:

In an assignment A(I) = B, the number of elements in B and I must be the same.
q(i,:)=[k2 l2];

I would be very thankful for any suggestion.

Shai
  • 111,146
  • 38
  • 238
  • 371
cheema
  • 39
  • 3
  • It is best [not to use `i` as a variable name in Matlab](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab). – Shai Aug 03 '14 at 15:18

1 Answers1

0

q(i,:)=[k2 l2]; does not work because both k2 and 12 are nx1 matrix and you are assigning it q(i,:), which should basically be row matrix. So q(i,:) = [k2' l2']; will be more accurate..

Just a suggestion, your code can be easier to if you calculate a and b inside the loop and do the other calculations outside the loop.

Code:

A = [1 ;2; 3; 4; 5; 6; 7];
A = [A ;8 ;9];
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • thanks lakesh but it is not working may be i did not explain you well. actually I have saved vector matrix like[1;2;3;4;5;6;7] and i want to add another vector matrix like [8;9]in a same sequence so that saved matrix would be [1;2;3;4;5;6;7;8;9] – cheema Aug 03 '14 at 19:05
  • @lakesh I think the OP wants a column vector. – beaker Aug 04 '14 at 00:11