i have a list of data and i have to select first 5th column of each row ..then simultaneously 6th column and 7th column
조회 수: 1 (최근 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에서 Scope Variables and Generate Names에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!