Hello. Could anyone tell me how to fix the for-loop code?
>>
b = {'Hello world'; 'Good morning';'Good afternoon';'6 years';'10+ years';'<1 year'}
z=array2table(b,'VariableNames', {'var1'})
for i=1:length(z)
if z(i,1)='Hello world'; z(i,1)=1;
else z(i,1)=0;
end
end
Output:
b =
6×1 cell array
{'Hello world' }
{'Good morning' }
{'Good afternoon'}
{'6 years' }
{'10+ years' }
{'<1 year' }
z =
6×1 table
var1
__________________
{'Hello world' }
{'Good morning' }
{'Good afternoon'}
{'6 years' }
{'10+ years' }
{'<1 year' }
if z(i,1)='Hello world'; z(i,1)=1;
Incorrect use of '=' operator. Assign a value to a variable using '=' and compare values for equality using '=='.

 채택된 답변

Star Strider
Star Strider 2021년 4월 23일

0 개 추천

Do exactly this:
compare values for equality using '=='
However the actual solution is likely the strcmp (or related strcmpi) function.
Try this:
b = {'Hello world'; 'Good morning';'Good afternoon';'6 years';'10+ years';'<1 year'}
z=array2table(b,'VariableNames', {'var1'})
for i=1:height(z)
if strcmp(z{i,1}, 'Hello world'); z{i,1}={1};
else z{i,1} = {0};
end
end
Indexing correctly (here using curly braces {}) is important.

댓글 수: 2

Oleksandr Puchak
Oleksandr Puchak 2021년 4월 23일
Star Strider, Thank you for pointing strcmp to me - it is very useful. Code that you suggest works after a slight adjustment. Thank you.
b = {'Hello world'; 'Good morning';'Good afternoon';'6 years';'10+ years';'<1 year'}
z=array2table(b,'VariableNames', {'var1'})
for i=1:height(z)
if strcmp(z{i,1}, 'Hello world')==1; z{i,1}={1};
else z{i,1} = {0};
end
end
Star Strider
Star Strider 2021년 4월 23일
My pleasure!

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

추가 답변 (1개)

DGM
DGM 2021년 4월 23일
편집: DGM 2021년 4월 23일

0 개 추천

You need to use == for equality tests, not =. That said, don't try testing character arrays for equality that way either. Unless they have the same number of characters, you'll have problems. Use strcmp(), strcmpi(), or ismember().
b = {'Hello world'; 'Good morning';'Good afternoon';'6 years';'10+ years';'<1 year';'0'}
z=array2table(b,'VariableNames', {'var1'})
for i=1:height(z) % use height/width/size/numel, not length
if strcmp(z{i,'var1'},'Hello world'); % use curly braces for indexing here. you can either use column number or name
z{i,'var1'}={'1'}; % i'm assuming that elements of a table column should share the same datatype
elseif strcmp(z{i,'var1'},'0'); % if there's a condition, use elseif instead of else
z{i,'var1'}={'something else'};
else
% do something else
end
end
z % see what changed
gives
z =
var1
________________
'Hello world'
'Good morning'
'Good afternoon'
'6 years'
'10+ years'
'<1 year'
'0'
z =
var1
________________
'1'
'Good morning'
'Good afternoon'
'6 years'
'10+ years'
'<1 year'
'something else'
I added an extra element to your list so that the second case was demonstrated.

댓글 수: 4

Oleksandr Puchak
Oleksandr Puchak 2021년 4월 23일
Dear DGM, Thank you for your reply. Do you if it matters if I use {'1'} vs {1} in ie.,
{i,'var1'}={'1'} vs {i,'var1'}={'1'} ?
Whihc option will make sure that the cell entry is a number?
DGM
DGM 2021년 4월 23일
if you use {1}, you're inserting a numeric value. If you use {'1'}, you're inserting a string. As I mentioned in the comments, a column in a table typically contains data of one type. There's nothing stopping you from mixing datatypes though. It all depends on what you want to do.
Oleksandr Puchak
Oleksandr Puchak 2021년 4월 23일
Dear, DGM. I understand. I want it to look like this., but Instead I get something like this
Oleksandr Puchak
Oleksandr Puchak 2021년 4월 23일
i need it to ne numerical so I can run the regression.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2021년 4월 23일

댓글:

2021년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by