i have a list of data and i have to select first 5th column of each row ..then simultaneously 6th column and 7th column

4 次查看(过去 30 天)
T = table(categorical({'karan';'varun';'ravina'}),[25;45;12],...
{'Surgery';'orthopaedic';'orthopaedic'},[2017-04-15;2017-04-16;2017-04-15],[5000:8000:8000],['M';'M';'F'],logical([1;0;0]),...
'VariableNames',{'Name','Age','Dept','DoB','charge','gender','vote'})
for i=1:3
for j=1:7
matrix(j)=T(i,j) %at this point i should assign each row to matrix(j)
end
first_task=matrix(j-2) % i should assign each row value of 'charge' here
I am not able to assign here...mistake in coding ...can anyone pls guide me where i am going wrong..
  7 个评论
prabhu singh
prabhu singh 2022-3-8
instead of creating a table form ..can i go with some other technique? (with same list of data)
like i can't create array bcoz each data is different

请先登录,再进行评论。

回答(3 个)

Arif Hoq
Arif Hoq 2022-3-6
编辑:Arif Hoq 2022-3-6
I have changed your variable from categorical to cell arrray.
T = table({'karan';'varun';'ravina'},[25;45;12],...
{'Surgery';'orthopaedic';'orthopaedic'},['2017-04-15';'2017-04-16';'2017-04-15'],...
[5000;8000;8000],['M';'M';'F'],logical([1;0;0]),...
'VariableNames',{'Name','Age','Dept','DoB','charge','gender','vote'});
T1=table2cell(T);
matrix=cell(size(T,1),1);
for i=1:3
matrix{i} = T1(i,:);
end
output=reshape([matrix{:}],7,3)'
output = 3×7 cell array
{'karan' } {[25]} {'Surgery' } {'2017-04-15'} {[5000]} {'M'} {[1]} {'varun' } {[45]} {'orthopaedic'} {'2017-04-16'} {[8000]} {'M'} {[0]} {'ravina'} {[12]} {'orthopaedic'} {'2017-04-15'} {[8000]} {'F'} {[0]}
  11 个评论
Arif Hoq
Arif Hoq 2022-3-7
Please be specific about your expectaion. you don't need for loop i guess. if you want charge or gender column only or all variable then you don't need loop. you can do it by linear indexing.
Here again i have tried in your way with foor loop(unnecessary) and check the display result(logical part).
again i am asking about your expectation.
T = table({'karan';'varun';'ravina'},[25;45;12],...
{'Surgery';'orthopaedic';'orthopaedic'},['2017-04-15';'2017-04-16';'2017-04-15'],...
[5000;8000;8000],['M';'M';'F'],logical([1;0;0]),...
'VariableNames',{'Name','Age','Dept','DoB','charge','gender','vote'});
T1=table2cell(T);
matrix=cell(size(T,1),3);
for i=1:3
% matrix{i,1} = T1(i,5); % charge column only
% matrix{i,2} = T1(i,6); % gender column only
matrix{i,3} = T1(i,:); % all
end
allVar=[matrix{:}];
results= reshape(allVar,7,[])';
out=cell2table(results);
first_task=results(:,5); % charge clolumn only
second_task=results(:,6); % gender clolumn only
for j=1:3
if table2array(out(j,7))== true
disp(results{j, 1})
else
disp(results{j, 3})
end
end
prabhu singh
prabhu singh 2022-3-7
sir my expectation is
i have attached the image below ..
i have to use loop here bcoz for every row i have to repeat the same procedure
first step is to assign 5th column ( one value of each row) then 6th column value
i.e my loop first should select first row from the table in that assign
first task=5000
second task='M'
if T(i,7)==true %logical one
then
function(first task) %i have created function here where i have to pass that first task value
else
otherfunction(first task) %i have created other function also here where same first task value is passed
then same procedure i have to follow for second task also
if T(i,7)== true %logical one
then
function(secondtask) %i have created function here where i have to pass that second task value
else
otherfunction(fsecondtask) %i have created other function also here where same second task value is passed

请先登录,再进行评论。


Jan
Jan 2022-3-6
With some guessing:
T = table(categorical({'karan';'varun';'ravina'}), ...
[25;45;12],...
{'Surgery';'orthopaedic';'orthopaedic'}, ...
["2017-04-15";"2017-04-16";"2017-04-15"], ...
[5000; 8000; 8000], ...
['M';'M';'F'], ...
logical([1;0;0]),...
'VariableNames', {'Name','Age','Dept','DoB','charge','gender','vote'});
matrix = table2cell(T(3, :))
matrix = 1×7 cell array
{[ravina]} {[12]} {'orthopaedic'} {["2017-04-15"]} {[8000]} {'F'} {[0]}

Peter Perkins
Peter Perkins 2022-3-7
You almost certainly do not need cell arrays. You certainly don't need numeric matrices because most of your data is not numeric.
Here's the table you want to start from:
>> T = table(categorical({'karan';'varun';'ravina'}), ...
>> [25;45;12],...
>> categorical({'Surgery';'orthopaedic';'orthopaedic'}), ...
>> datetime({'2017-04-15';'2017-04-16';'2017-04-15'}), ...
>> [5000;8000;8000], ...
>> categorical({'M';'M';'F'}), ...
>> logical([1;0;0]), ...
>> 'VariableNames',{'Name','Age','Dept','DoB','charge','gender','vote'})
T =
3×7 table
Name Age Dept DoB charge gender vote
______ ___ ___________ ___________ ______ ______ _____
karan 25 Surgery 15-Apr-2017 5000 M true
varun 45 orthopaedic 16-Apr-2017 8000 M false
ravina 12 orthopaedic 15-Apr-2017 8000 F false
Now, what is it that you want to do? "select first 5th column of each row ..then simultaneously 6th column and 7th column"? That's just
>> T(1,5:7)
ans =
1×3 table
charge gender vote
______ ______ _____
5000 M true
But what you you want to do with that? You almost certainly do not need cell arrays or loops.

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by