MATLAB Answers


How to save, for loop

Asked by Hello kity on 14 Dec 2012


for i=1:n X=DATA1(i,1); Y=DATA2{1,i}; Z=X-Y;


what I want is that it saves the data to Z, summing them, so, for i=1 , Z=[1 2 3 4 5], for i=2 Z becomes Z=[1 2 3 4 5 6 7] etc.

so i need to do something with Z but dont know what. currently it only shows the result of the last i.


Do you really have X as a numerical array (double, etc.) and Y as a cell array???

yes x= column full of values (nummerical array) and Y cells , with each cell with diff lengths



1 Answer

Answer by Pedro Villena
on 14 Dec 2012
Edited by Pedro Villena
on 14 Dec 2012
 Accepted answer

 for i=1:n,
    X = DATA1(i,1); %% DATA1 is an array
    Y = DATA2{i,1}; %% DATA2 is a cell
    Z(i,:) = X-Y;
 save Z


 Z = DATA1 + cell2mat(DATA2);
 save Z

  1 Comment

the first one works if both X and Y are matrix ( ()) but if Y is cell array {} then it gives trouble. Somehow I need to convert Y to matrix then put it in the equation.

I look into the second tip

thank you

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!