필터 지우기
필터 지우기

from a python code to matlab code

조회 수: 2 (최근 30일)
Michael Angeles
Michael Angeles 2021년 12월 30일
댓글: Michael Angeles 2021년 12월 31일
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))

채택된 답변

Image Analyst
Image Analyst 2021년 12월 31일
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);
255
average_price = total_price/numel(prices);
fprintf('Average Hair Cut Price is: %.3f\n', average_price);
Average Hair Cut Price is: 31.875
%problem #5
new_prices = prices()-5;
disp(new_prices);
25 20 35 15 15 30 45 30
%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);
Total Revenue is: 1085
%average revenue
average_revenue = total_revenue/7;
fprintf('The average revenue is: %.f\n', average_revenue)
The average revenue is: 155
% 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)
cuts_under_30 = 1×4 cell array {'bouffant'} {'pixie'} {'crew'} {'bowl'}
prices_under_30 = new_prices(indexes)
prices_under_30 = 1×4
25 20 15 15
fprintf('Cuts under 30 :\n')
Cuts under 30 :
for k = 1 : length(prices_under_30)
fprintf('%s costs $%.2f\n', cuts_under_30{k}, prices_under_30(k));
end
bouffant costs $25.00 pixie costs $20.00 crew costs $15.00 bowl costs $15.00
fprintf('\n')
  댓글 수: 1
Michael Angeles
Michael Angeles 2021년 12월 31일
Thank you so much...was trying to figure that out for a while...

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Chunru
Chunru 2021년 12월 31일
% cuts_under_30 = [hairstyles[i] for i in range(len(hairstyles)) if new_prices[i] < 30]
cuts_under_30 = hairstyles(new_prices < 30);
  댓글 수: 3
Chunru
Chunru 2021년 12월 31일
Or simply remove the semicolon at the end of the statement.
Michael Angeles
Michael Angeles 2021년 12월 31일
편집: Image Analyst 2021년 12월 31일
It didn't work....Here is the whole code I am working on from Python...
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
%cuts_under_30 = ;
% for cuts_under_30 = 1:numel(hairstyles)
% i = hairstyles(cuts_under_30);
% if new_prices < 30
% end
% disp(cuts_under_30)
fprintf("Cuts under 30 :\n")
fprintf('%.2f ', cuts_under_30);
fprintf('\n')

댓글을 달려면 로그인하십시오.


Chunru
Chunru 2021년 12월 31일
편집: Chunru 2021년 12월 31일
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)
total_price = 255
average_price = mean(prices)
average_price = 31.8750
new_prices = prices()-5;
total_revenue = sum(prices.*last_week)
total_revenue = 1085
average_revenue = total_revenue/7
average_revenue = 155
cuts_under_30 = last_week(new_prices < 30) % the number of haircuts under $30
cuts_under_30 = 1×4
2 3 8 4
hairstyles (new_prices < 30) % the styles of haircuts under $30
ans = 1×4 string array
"bouffant" "pixie" "crew" "bowl"
new_prices(new_prices < 30)
ans = 1×4
25 20 15 15

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by