for all iteration the function ricerca sometimes gives 7 values, sometimes 2
how can i insert all the values in a matrix. It's all ok for the first 8 iterations where the functions ricerca gaves always 7 values. with the 8th iteration where from ricerca I get less values it gives me the error
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
i=1;
for S=0.05:0.05:1
LIV(i,:)=ricerca(V0,W,S,N)
i=i+1
end

 Respuesta aceptada

Adam Danz
Adam Danz el 28 de Mzo. de 2020
Editada: Adam Danz el 28 de Mzo. de 2020

0 votos

You can either store the outputs in a cell array like this
i=1;
for S=0.05:0.05:1
LIV{i}=ricerca(V0,W,S,N)
% ^ ^
i=i+1
end
or you can pad the shorter vectors with NaN values so they match the correct size
i=1;
for S=0.05:0.05:1
results = ricerca(V0,W,S,N);
results = [results, nan(1,8-numel(results))]; %pad with NaN at the end
LIV(i,:) = results;
i=i+1
end
Don't forget to pre-allocate your LIV variable!

4 comentarios

Simo
Simo el 28 de Mzo. de 2020
with the first method how can I then use the values in LIV matrix? For examples plot the first values or the second (how i write in the code below)
the second method gaves me
Unrecognized function or variable 'resutls'.
Error in Ex2_18_bella (line 45)
LIV(i,:) = resutls;
what do you mean by preallocate?
S=0.05:0.05:1
plot(S,LIV(:,1),S,LIV(:,2))
Adam Danz
Adam Danz el 28 de Mzo. de 2020
"with the first method how can I then use the values in LIV matrix"
First, please see a small change I made (I changed LIV{i,:} to LIV{i}).
To access the results from iteration 'n', LIV{n}.
"he second method gaves me Unrecognized function or variable 'resutls'."
That's because of a typo in my answer. I've edited it. I changed "resutls" to "results".
Simo
Simo el 28 de Mzo. de 2020
I've also thinked this way, but it says "Dimensions of arrays being concatenated are not consistent."
A=0.05:0.05:1;
for S=0.5:0.05:1
LIV=ricerca(V0,W,S,N);
A=[A;LIV];
end
Adam Danz
Adam Danz el 28 de Mzo. de 2020
Editada: Adam Danz el 28 de Mzo. de 2020
'A' is 1x20 vector. You said the output of ricerca was either 8 or 2 elements long. Matrices need to have the same number of columns across all rows and the same number of rows across all columns.
You could store it in a cell array
i=1;
for S=0.05:0.05:1
LIV{1,i}=ricerca(V0,W,S,N)
LIV{2,i} = A; % <----
i=i+1
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 28 de Mzo. de 2020

Editada:

el 28 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by