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 个评论
回答(3 个)
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)'
11 个评论
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
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, :))
0 个评论
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!