How to save data in a vector for each loop indice?

조회 수: 2 (최근 30일)
Tanya Sharma
Tanya Sharma 2021년 8월 24일
댓글: Tanya Sharma 2021년 8월 31일
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

채택된 답변

Turlough Hughes
Turlough Hughes 2021년 8월 24일
편집: Turlough Hughes 2021년 8월 24일
x = 1:0.1:1.5;
vec=zeros(size(x));
for ii = 1:numel(x)
sol = x(ii)+1;
vec(ii)=sol;
end
vec
vec = 1×6
2.0000 2.1000 2.2000 2.3000 2.4000 2.5000
  댓글 수: 5
Turlough Hughes
Turlough Hughes 2021년 8월 25일
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
Tanya Sharma
Tanya Sharma 2021년 8월 31일
Thank you for the support Turlough. This solved my problem. Highly appreciate it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by