A beginners question on reading matrix array

I'm learning MATLAB out of curiosity, without any prior programming experience. I'm having an issue with a simple matrix array.

What_to_Wear
%
% 	out1 = What_to_wear({'pants','scarf','heels','shoes'; true,false,true,true; true,true,false,true})
%       out1 => ['You should wear the pants and/or shoes, but DO NOT wear the scarf or heels.']
%

So, my array is

Pants scarf heels shoes
true  false true  true 
true  true  false true 

when R2C1 = R3C1 = True, True - then yes to "pants" else no.

Similarly, when R2C3=R3C3 True, True then yes to shoes.

In other words when row 2 and row 3 are both true, you should wear the item in row 1, otherwise you should not wear the item in row 1.

I hope I was able to clearly present the problem.

Thanks in advance.

Antworten (3)

Image Analyst
Image Analyst am 13 Okt. 2015

0 Stimmen

How about this:

ca = cell({'pants','scarf','heels','shoes'; true,false,true,true; true,true,false,true})
for col = 1 : size(ca, 2)
	if ca{2, col} & ca{3, col}
		fprintf('You should wear the %s.\n', ca{1, col});
	else
		fprintf('You should not wear the %s.\n', ca{1, col});
	end
end

gives:

ca = 
    'pants'    'scarf'    'heels'    'shoes'
    [    1]    [    0]    [    1]    [    1]
    [    1]    [    1]    [    0]    [    1]
You should wear the pants.
You should not wear the scarf.
You should not wear the heels.
You should wear the shoes.

2 Kommentare

v kay
v kay am 13 Okt. 2015
thanks but the output I was expecting is more like "You should wear the pants and/or shoes, but DO NOT wear the scarf or heels." thanks a lot for your help though.

Melden Sie sich an, um zu kommentieren.

Stephen23
Stephen23 am 13 Okt. 2015
Bearbeitet: Stephen23 am 13 Okt. 2015

0 Stimmen

C = {'pants','scarf','heels','shoes'; true,false,true,true; true,true,false,true};
% Index of where both lines are true:
idx = all(cell2mat(C(2:3,:)),1);
X(2,:) = C(1,idx); % clothes for true
Y(2,:) = C(1,~idx); % clothes for false
% Add punctuation:
X(1,:) = {', '};
Y(1,:) = {', '};
X{1,end} = ' and/or ';
Y{1,end} = ' or ';
% Create output string:
fmt = 'You should wear the %s, but DO NOT wear the %s.';
out = sprintf(fmt,[X{2:end}],[Y{2:end}])
displays this string in the command window:
out = You should wear the pants and/or shoes, but DO NOT wear the scarf or heels.
Guillaume
Guillaume am 13 Okt. 2015
Bearbeitet: Guillaume am 13 Okt. 2015

0 Stimmen

There are many ways to obtained the desired output. One possible way
clothes = {'pants', 'scarf', 'heels','shoes';
true, false, true, true;
true, true, false, true};
towear = all(cell2mat(clothes([2 3], :)));
if any(towear)
%at least one item is to be worn
wornclothes = ['the ' strjoin(clothes(1, towear), ' and/or ')];
else
wornclothes = 'nothing';
end
sentence = sprintf('You should wear %s', wornclothes);
if all(towear)
%if everything has to be worn, there's no 2nd part to the sentence
sentence = [sentence '.'];
else
sentence = sprintf('%s but DO NOT wear the %s.', sentence, strjoin(clothes(1, ~towear), ' nor the '));
end
fprintf('%s\n', sentence);
The above neatly demonstrates logical indexing, all and any, and different methods of constructing strings.

Kategorien

Gefragt:

am 12 Okt. 2015

Bearbeitet:

am 13 Okt. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by