how to view changed matrix
古いコメントを表示
hi,
i have attached a code below
clc
clear
[~,~,A]=xlsread('duplicateset1.xls');
%A(isnan(A))=-1;
cellfun(@isnan,A,'UniformOutput',false)%to remove NaN from array
[m,n]=size(A);
B=A;
%change Y/N to 0/1
for i=1:n
if isequal(A{i},'Y')||isequal(A{i},'M')
B(:,i)=1;
elseif isequal(A{i},'N')||isequal(A{i},'F')
B(:,i)=0;
elseif isequal(A{i},'U')||isequal(A{i},'H')
B(:,i)=2;
elseif isequal(A{i},'C')||isequal(A{i},'D')||isequal(A{i},'L')
B(:,i)=-1;
end
end
B(1:10,3)
i want the B matix to be of 1's and 0's instead of yes and no, but what i get is the intial matrix. the other chararcter represent male/female/high/low etc
6 件のコメント
David Hill
2019 年 10 月 3 日
I don't understand your question. If you just want to display what is in array B, just type B in the command window and hit return or double click on B in the workspace.
Adam
2019 年 10 月 3 日
xlswrite is the usual companion function to xlsread for outputting to spreadsheet, although I noticed both are now listed as 'not recommended' (in R2019b at least). Alternatives are given on their respective help pages though.
the cyclist
2019 年 10 月 3 日
Not answering your question, but just wanted to point out that your series of if-elseif statement is tailor-made for using the switch-case construct instead:
for i = 1:n
switch A{i}
case {'Y','M'}
B(:,i)=1;
case {'N','F'}
B(:,i)=0;
case {'U','H'}
B(:,i)=2;
case {'C','D','L'}
B(:,i)=-1;
otherwise
disp('Unexpected character')
end
end
the cyclist
2019 年 10 月 5 日
If you upload your input file, it might be easier to understand what you mean
Chandini Govind
2019 年 10 月 6 日
Deepak Kumar
2019 年 10 月 9 日
You are not capturing the values returned by cellfun in a variable (e.g. z=cellfun(@isnan,A,'UniformOutput',false)). So, the values returned by cellfun is lost and A remains unaffected and retains it's original values. That is why you are getting the original values of A. Moreover, please go through the documentation of isnan command (https://www.mathworks.com/help/matlab/ref/isnan.html ) to understand it's usage.
回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!