HELLO! i am trying to save a matrix on each loop.
i know this is simple syntax but it is not working for me. This is the code i have where i know there is a syntax error. it just keeps overriding the previous matrix.
while(fin)
% made seperate matrix for the test
for i=1:length(pts)
matrix = [x(next:pts(i,:)), y(next:pts(i,:)), test(next:pts(i,:))];
next = pts(i);
end
if pts(i) == length(pts)
fin = 0;
end
return
end
i then attempted to add this, and this is the error coming up.
test(i,:) = matrix;
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
i have attempted to use matrix{i} but that gives me a cell and i cant handle it the way i want to. none of the cell commands work...
i have also tried matrix(:,i) = [x(next:pts(i,:)), y(next:pts(i,:)), test(next:pts(i,:))];
Unable to perform assignment because the indices on the left side are not compatible with
the size of the right side.
Error in arrayplay (line 56)
matrix(:,i) = [x(next:pts(i,:)), y(next:pts(i,:)), num_step(next:pts(i,:))];
please help. im just about ready to cry.

 Réponse acceptée

KSSV
KSSV le 11 Mar 2021

0 votes

You can save them into a cell as shown below.
iwant = cell([],1) ; % initilize a cell array;
while(fin)
% made seperate matrix for the test
for i=1:length(pts)
matrix = [x(next:pts(i,:)), y(next:pts(i,:)), test(next:pts(i,:))];
next = pts(i);
iwant{i} = matrix ; % save into a cell
end
if pts(i) == length(pts)
fin = 0;
end
return
end
Access the cell using: iwant{1}, iwant{2},...etc.
You can also initialize it into amatrix, if dimensions are known in prior and if dimensions don't change in a loop.

5 commentaires

caroline bourn
caroline bourn le 11 Mar 2021
thanks! so the next step is writing a code that will go through the contents of each cell and do whatever to it. is there a way to do it simply?
KSSV
KSSV le 11 Mar 2021
If the dimensions remains same, you can save them into a matrix or you can also convert cell array into matrix using Cell2mat.
caroline bourn
caroline bourn le 11 Mar 2021
the dimensions are not the same :( is there a better way?
KSSV
KSSV le 11 Mar 2021
Modifié(e) : KSSV le 11 Mar 2021
Then go by cell array....if problem is well known there might be another easy way.
caroline bourn
caroline bourn le 11 Mar 2021
im sorry, i don't understand. could you explain it a bit further?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Performance and Memory dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by