How to create string of text-objects

Hi, I have the following matrix as an output. I wouldlike to make a third column such that the rows corresponding to 1, i would like to write "paid" and the rows corresponding to 0 I would like to write "not paid" so that the resulting table should have three columns, the first two columns are columns of the matrix D and the third should contain either "paid" or "not paid". I have inserted the expected output. The second matrix I have inesrted the column mannualy. Please help.
D =
13 0
4 1
5 1
1 1
2 1
3 1
14 0
5 1
16 0
7 1
8 1
9 1
3 1
4 1
2 1
1 1
3 1
98 0
100 0
what i expect is;
D =
13 0 Notpaid
4 1 paid
5 1 paid
1 1 paid
2 1 paid
3 1 paid
14 0 Notpaid
5 1 paid
16 0 Notpaid
7 1 paid
8 1 paid
9 1 paid
3 1 paid
4 1 paid
2 1 paid
1 1 paid
3 1 paid
98 0 Notpaid
100 0 Notpaid

 Accepted Answer

dpb
dpb on 26 Apr 2023
Edited: dpb on 26 Apr 2023
Holding disparate data types would be a good place to use a table as alternative to the cell array...
D = [randi(100,10,1),rand(10,1)>=0.5];
tD=array2table(D,'VariableNames',{'Amount','PayStatus'}); % convert to table
CODE=categorical({'Notpaid';'Paid'}); % the lookup values
tD=addvars(tD,CODE(tD.PayStatus+1),'NewVariableNames',{'BillStatus'}) % add the verbal status
tD = 10×3 table
Amount PayStatus BillStatus ______ _________ __________ 71 0 Notpaid 65 0 Notpaid 77 0 Notpaid 49 1 Paid 80 1 Paid 73 1 Paid 41 1 Paid 8 0 Notpaid 74 0 Notpaid 26 0 Notpaid

More Answers (2)

You can't concatenate doubles and string in one array, because the array can be only one of them.
If you want to only store the information you can use cells:
D = [randi(100,20,1),rand(20,1)>=0.5];
result = cell(2,1);
result{1} = D;
result{2} = strings(length(D),1);
for j = 1 : length(D)
if ~D(j,2)
result{2}(j) = "Notpaid";
else
result{2}(j) = "Paid";
end
end
If it's for display purposes you can transform everything into a string:
D = [randi(100,20,1),rand(20,1)>=0.5];
result = strings(length(D),3);
result(:,1:2) = string(D);
for j = 1 : length(D)
if ~D(j,2)
result(j,3) = "Notpaid";
else
result(j,3) = "Paid";
end
end

4 Comments

To eliminate the loop construct and explicit if...else clause, "the MATLAB way" would be to use a lookup table and vectorize...
CODE={'Notpaid';'Paid'}; % the lookup values
D=num2cell(D); % convert original to cell array to add disparate type
D(:,3)=CODE([D{:,2}]+1) % add the third column based on second [0,1]==>[1,2] index
D = 10×3 cell array
{[86]} {[1]} {'Paid' } {[43]} {[0]} {'Notpaid'} {[45]} {[1]} {'Paid' } {[17]} {[1]} {'Paid' } {[51]} {[0]} {'Notpaid'} {[42]} {[1]} {'Paid' } {[66]} {[0]} {'Notpaid'} {[ 5]} {[1]} {'Paid' } {[93]} {[0]} {'Notpaid'} {[95]} {[0]} {'Notpaid'}
My usual way of doing it would be to:
paidIdx = D(:,2) == 1;
D(paidIdx,3) = "Paid";
D(~paidIdx,3) = "Notpaid";
But your approach is much better.
I don't know that it's "much" better, but does illustrate using the lookup table approach that many novices may have not seen...it trades one temporary variable for another so isn't a tremendous advantage one way or another other than, perhaps, putting the constants into one array so can just adjust it if number of choices changes.
Aesthetically much better*

Sign in to comment.

Given the format of your final output, you will either have to use string arrays, cell arrays or categorical arrays.
%Categorical array method
D =[13 0
4 1
5 1
1 1
2 1
3 1
14 0
5 1
16 0
7 1
8 1
9 1
3 1
4 1
2 1
1 1
3 1
98 0
100 0];
%Values corresponding to paid
idx = D(:,2);
%Convert D to a categorical array
D = categorical(D);
str = categorical(["Notpaid", "paid"]);
D(:,3) = str(idx+1)
D = 19×3 categorical array
13 0 Notpaid 4 1 paid 5 1 paid 1 1 paid 2 1 paid 3 1 paid 14 0 Notpaid 5 1 paid 16 0 Notpaid 7 1 paid 8 1 paid 9 1 paid 3 1 paid 4 1 paid 2 1 paid 1 1 paid 3 1 paid 98 0 Notpaid 100 0 Notpaid

1 Comment

Actually, the categorical is better choice than the string... +1

Sign in to comment.

Categories

Products

Release

R2018a

Asked:

on 26 Apr 2023

Commented:

on 26 Apr 2023

Community Treasure Hunt

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

Start Hunting!