How to save data in a vector for each loop indice?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Tanya Sharma
il 24 Ago 2021
Commentato: Tanya Sharma
il 31 Ago 2021
I am unable to save the data for each loop entry to a vector, as it gives the error that array indices must be integers. How do I save data in vec for each indice of 'ii'?
clear;
clc;
ii=1:0.1:1.5;
vec=zeros(length(ii),1);
for x = ii
sol = x+1;
vec(ii)=sol;
end
0 Commenti
Risposta accettata
Turlough Hughes
il 24 Ago 2021
Modificato: Turlough Hughes
il 24 Ago 2021
x = 1:0.1:1.5;
vec=zeros(size(x));
for ii = 1:numel(x)
sol = x(ii)+1;
vec(ii)=sol;
end
vec
5 Commenti
Turlough Hughes
il 25 Ago 2021
Another way would be to use a seperate counter:
ii=1:0.1:1.5;
vec=zeros(size(ii));
jj = 1;
for x = ii
sol = x+1;
vec(jj)=sol;
jj = jj + 1;
end
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!