from a python code to matlab code
2 views (last 30 days)
Show older comments
Michael Angeles
on 30 Dec 2021
Commented: Michael Angeles
on 31 Dec 2021
how can I convert the following code to matlab code?
cuts_under_30 = [hairstyles[i] for i in range(len(hairstyles)) if new_prices[i] < 30]
print("Cuts under 30: " "{}".format(cuts_under_30))
0 Comments
Accepted Answer
Image Analyst
on 31 Dec 2021
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
%workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
hairstyles = {'bouffant', 'pixie', 'dreadlocks', 'crew', 'bowl', 'bob', 'mohawk', 'flattop'};
prices = [30, 25, 40, 20, 20, 35, 50, 35];
last_week = [2, 3, 5, 8, 4, 4, 6, 2];
%my code
total_price = 0;
for price = 1:length(prices)
total_price = total_price + prices(price);
end
disp(total_price);
average_price = total_price/numel(prices);
fprintf('Average Hair Cut Price is: %.3f\n', average_price);
%problem #5
new_prices = prices()-5;
disp(new_prices);
%fprintf('The new prices are: %.2f\n', new_prices);
%Revenue
total_revenue = 0;
total_revenue = sum(prices.*last_week);
fprintf('Total Revenue is: %.f\n', total_revenue);
%average revenue
average_revenue = total_revenue/7;
fprintf('The average revenue is: %.f\n', average_revenue)
% cuts_under_30 = [hairstyles[i] for i in range(len(hairstyles)) if new_prices[i] < 30]
indexes = new_prices < 30;
cuts_under_30 = hairstyles(indexes)
prices_under_30 = new_prices(indexes)
fprintf('Cuts under 30 :\n')
for k = 1 : length(prices_under_30)
fprintf('%s costs $%.2f\n', cuts_under_30{k}, prices_under_30(k));
end
fprintf('\n')
More Answers (2)
Chunru
on 31 Dec 2021
% cuts_under_30 = [hairstyles[i] for i in range(len(hairstyles)) if new_prices[i] < 30]
cuts_under_30 = hairstyles(new_prices < 30);
3 Comments
Chunru
on 31 Dec 2021
Edited: Chunru
on 31 Dec 2021
hairstyles = ["bouffant", "pixie", "dreadlocks", "crew", "bowl", "bob", "mohawk", "flattop"];
prices = [30, 25, 40, 20, 20, 35, 50, 35];
last_week = [2, 3, 5, 8, 4, 4, 6, 2];
total_price = sum(prices)
average_price = mean(prices)
new_prices = prices()-5;
total_revenue = sum(prices.*last_week)
average_revenue = total_revenue/7
cuts_under_30 = last_week(new_prices < 30) % the number of haircuts under $30
hairstyles (new_prices < 30) % the styles of haircuts under $30
new_prices(new_prices < 30)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!