function for looking for strings in table

randomnumbers = 5 + (10-5)*rand(71,1);
randomnumbers2 = 10 + (20-10)*rand(71,1);
randomnumbers3 = 100 + (200-100)*rand(71,1);
for j = 1:size(table)
if table.Var1(j) == "a"
table.Result1(j) = 0;
elseif table.Var1(j) == "b"
if table.Mode(j) == "monday"
table.Result1(j) = randomnumbers2(j)
elseif table.Mode(j) == "thursday"
table.Result1(j) = randomnumbers2(j)
else
table.Result1(j) = randomnumbers3(j)
end
else
break
end
end
for j = 1:size(table)
if table.Var2(j) == "a"
table.Result2(j) = 0;
elseif table.Var2(j) == "b"
if table.Mode(j) =="monday"
table.Result2(j) == randomnumbers2(j)
elseif table.Mode(j) == "thursday"
table.Result2(j) = randomnumbers2(j)
else
table.Result2(j) = randomnumbers3(j)
end
else
break
end
end
Hello, this is pieece of my code.
I have a table of size 71x15. I want to check if variables stored in columns 2:10 are either "a", "b" or "c". If they are a, I would like to assign the value of 0 in new column. If they are B, I would like to assign the value from array randomnumbers 2/random numbers 3, depending on which day is stored in column "mode" and if they are "c" the code should do nothing and move to the next row.
The problem is that, for the column Var1, this loop presented above works fine. If I repeat the same loop for respectively Var2, Var, Var4...Var10, all the numbers assigned are zero.
Could you please tell me how to fix this, and more importantly, if there exists a possibility to create function from that that does it whenever we call it? As you can see, its inconvinient to crete 9 loops like this, to check for those strings in columns from Var2...Var10.
Thanks in advance!

댓글 수: 8

Where do you assign a value to j ?
I have corrected it. I was never intended to use k in this loop.
What is size() of table? Is it a scalar? If it is not a scalar, then how does the : operator behave when used with a non-scalar ?
Kacper Witasinski
Kacper Witasinski 2022년 2월 21일
편집: Kacper Witasinski 2022년 2월 21일
size(table) = [71,15]
What I meant by this conidition " j = 1:size(table)" was to execute this code for every cell in the table. To look for those values in every cell.
randnumbers2 = rand(3, 1)
randnumbers2 = 3×1
0.3262 0.6881 0.7094
randnumbers3 = rand(3, 1)
randnumbers3 = 3×1
0.1059 0.1615 0.6308
Table = table()
Table = 0×0 empty table
Table.Col1 = rand(3, 1)
Table = 3×1 table
Col1 ________ 0.31658 0.021675 0.38292
Table.Col2 = string(('a' : 'c').')
Table = 3×2 table
Col1 Col2 ________ ____ 0.31658 "a" 0.021675 "b" 0.38292 "c"
% what would the result be for the above?
The break you have stops executing the loop as soon as you encounter the first entry that is not "a" or "b"
Okay, problem is solved. Thank You @Walter Roberson and @madhan ravi for explaining possible mistakes.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

2022년 2월 21일

댓글:

2022년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by