MATLAB应用实战系列(101)-新年新祝福,教你如何用MATLAB以对联的形式打开2022年,爱满人间(附MATLAB和Python代码实现)

Posted 文宇肃然

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MATLAB应用实战系列(101)-新年新祝福,教你如何用MATLAB以对联的形式打开2022年,爱满人间(附MATLAB和Python代码实现)相关的知识,希望对你有一定的参考价值。

前言

2022年迈着轻盈的脚步到来了,在这辞旧迎新的时光里,我想给大家拜个早年,用代码带给搞技术的小伙伴一点点欢乐,告慰即将逝去的2021年。

本文的代码分为MATLAB和Python两个版本,照顾到有不同编程语言使用习惯的小伙伴。

先把效果图放上来,本文的内容参考天元浪子博主的部分代码。

MATLAB代码

MATLAB代码包含两个动态效果,一个是燃放烟花,然后对联跳出效果。

燃放烟花

如墨的夜空,烟花(礼花)朵朵绽放,瞬间绚丽至极,迸射出璀璨夺目的光彩。只是还来不及在脑海中印上花魂,她已昙花一现般的消逝了。多少人痴迷于烟花的美,痴迷于她飘忽不定的幻影。

function firework()

% Created on Tue Jan 11 17:05:41 2022

% author: wenyusuran

OldHandle = findobj( 'Type', 'figure', 'Tag', 'FireWork' ) ;

if ishandle(OldHandle)

close(OldHandle) ;

end

% 图形窗口初始化

fig = figure('units','normalized','position',[0.1 0.1 0.8 0.8],'numbertitle','off','color',[0 0 0],'tag','FireWork');

% 烟花炸开前的初始位置

h0 = line(0,0,0,'marker','.','markersize',18,'MarkerEdgeColor',[1 1 1],'MarkerFaceColor',[1 1 1],'EraseMode' , 'xor','color',[0 0 0]);

% 设置坐标系显示属性

axis equal

axis([-50 50 -50 50 0 100])

axis off

view(-42,22)

% 设置参数

rate = 1:-0.01:0;  % 颜色衰减率

v0 = 45;  % 烟花头初始速度

g = -9.8;   % 重力加速度

usedtime = -v0/g;  % 烟花头爆炸前所经历时间

zs = v0*usedtime+0.5*g*usedtime^2;  %烟花头爆炸前达到的最高高度

theta = rand(250,1)*2*pi;  % 各粒子速度的方位角

phi =  rand(250,1)*2*pi-pi;  % 各粒子速度的仰角

age = 20;  % 粒子生存期

% 常用颜色矩阵

colormat = [1.0  0.5  0.5

1.0  0.75  0.5

1.0  1.0  0.5

0.75  1.0  0.5

0.5  1.0  0.5

0.5  1.0  0.75

0.5  1.0  1.0

0.5  0.75  1.0

0.5  0.5  1.0

0.75  0.5  1.0

1.0  0.5  1.0

1.0  0.5  0.75];

% 随机产生各粒子对应的颜色序号

colorid = randsample(12,250,true);

% 粒子对应的颜色矩阵

colormat = colormat(colorid,:);

% 粒子颜色与背景色(夜色)的距离

colordist = sqrt(sum(colormat.^2,2));

v1 = 20;  % 粒子的初始速度

k = 1;   % 颜色衰减率初始序号

timerA = timer('TimerFcn',@TimerFcnA,'executionmode','fixedspacing','Period',0.001);

start(timerA);

h = getappdata(gcf,'HandleParticle');

timerB = timer('TimerFcn',@TimerFcnB,'executionmode','fixedspacing','period',0.001);

%--------------------------------------------------------------------------

%  定时器回调函数(烟花头)

%--------------------------------------------------------------------------

function TimerFcnA(timerA,event)

ta = get(timerA,'TasksExecuted')*0.1;

if ta <= usedtime

z = v0*ta+0.5*g*ta^2;

set(h0,'zdata',z,'MarkerEdgeColor',[1 1 1],'MarkerFaceColor',[1 1 1])

drawnow

%pause(0.01)

else

delete(h0)

stop(timerA);

x0 = zeros(2,250);

y0 = zeros(2,250);

z0 = zs*ones(2,250);

h = line(x0,y0,z0,'marker','h','markersize',12,'MarkerEdgeColor',[1 1 1],'MarkerFaceColor',[1 1 1],'EraseMode' , 'xor','color',[0 0 0]);

setappdata(gcf,'HandleParticle',h);

start(timerB);

end

end

%--------------------------------------------------------------------------

%  定时器回调函数(粒子)

%--------------------------------------------------------------------------

function TimerFcnB(timerB,event)

tb = get(timerB,'TasksExecuted')*0.15;

if age>0 && any(colordist>=0.05)

colormat = colormat*rate(k);

colordist = sqrt(sum(colormat.^2,2));

for i = 1:250

xi = v1*cos(phi(i))*cos(theta(i))*tb;

yi = v1*cos(phi(i))*sin(theta(i))*tb;

zi = zs+v1*sin(phi(i))*tb+0.5*g*tb^2;

set(h(i),'XData',xi,'YData',yi,'ZData',zi,'MarkerEdgeColor',colormat(i,:),'MarkerFaceColor',colormat(i,:))

end

drawnow

age = age-0.1;

k = k+1;

else

stop(timerB);

delete(h)

end

end

%-------------------

end

对联效果图代码

效果图如下:

function SpringScrolls(stringName,type,fontSize)
%stringName is the parameter matrix of string.
%the type can be 'rows' or 'cols'.
% Created on Tue Jan 11 17:05:41 2022

% author: wenyusuran
warning off
string=stringName;
if nargin<2
    type='rows';
end
switch 1
    case ~isempty(regexpi(string,',')),pause_location=regexpi(string,',');
    case ~isempty(regexpi(string,',')),pause_location=regexpi(string,',');
    case ~isempty(regexpi(string,' ')),pause_location=regexpi(string,' ');   
end
len_string=length(string);
if nargin<3
    if strcmp(type,'rows'),fontSize=25+0.002*30/max(2/1080,max(pause_location-1,len_string-pause_location)/1920);end
    if strcmp(type,'cols'),fontSize=20+0.002*30/max(2/1920,max(pause_location-1,len_string-pause_location)/1080);end
end
disp(fontSize);
BG=[];BG_cell=imread('背景图.jpg');
[m,n,~]=size(BG_cell);
for i=1:2
    for j=1:max(pause_location-1,len_string-pause_location)
        if strcmp(type,'rows'),BG((i-1)*(m+10)+1:(i-1)*(m+10)+m,(j-1)*n+1:j*n,:)=BG_cell(:,:,:);end
        if strcmp(type,'cols'),BG((j-1)*n+1:j*n,(i-1)*(m+10)+1:(i-1)*(m+10)+m,:)=BG_cell(:,:,:);end
    end
end
if strcmp(type,'rows'),BG(m+1:m+10,1:j*n,:)=0.94*255*ones(10,j*n,3);end
if strcmp(type,'cols'),BG(1:j*n,m+1:m+10,:)=0.94*255*ones(j*n,10,3);end
BG=uint8(BG);
imshow(BG)
hold on;
for k=1:pause_location-1
    if strcmp(type,'rows'),text((k-1)*m+m/2,n/2,string(k),'fontsize',fontSize,'FontName','华文行楷','HorizontalAlignment', 'center');end
    if strcmp(type,'cols'),text(n/2,(k-1)*m+m/2,string(k),'fontsize',fontSize,'FontName','华文行楷','HorizontalAlignment', 'center');end
end
for k=1:len_string-pause_location
    if strcmp(type,'rows'),text((k-1)*m+m/2,n+10+n/2,string(pause_location+k),'fontsize',fontSize,'FontName','华文行楷','HorizontalAlignment', 'center');end
    if strcmp(type,'cols'),text(n+10+n/2,(k-1)*m+m/2,string(pause_location+k),'fontsize',fontSize,'FontName','华文行楷','HorizontalAlignment', 'center');end
end
get(text)
end

Python代码

效果图如下:

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 11 17:05:41 2022

@author: wenyusuran
"""

import io
from PIL import Image
import numpy as np
import requests
def get_word(ch, quality):
    """获取单个汉字(字符)的图片
    ch          - 单个汉字或英文字母(仅支持大写)
    quality     - 单字分辨率,H-640像素,M-480像素,L-320像素
    """
    
    fp = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data='ch':ch).content)
    im = Image.open(fp)
    w, h = im.size
    if quality == 'M':
        w, h = int(w*0.75), int(0.75*h)
    elif quality == 'L':
        w, h = int(w*0.5), int(0.5*h)
    
    return im.resize((w,h))
def get_bg(quality):
    """获取春联背景的图片"""
    
    return get_word('bg', quality)
def write_couplets(text, HorV='V', quality='L', out_file=None):
    """生成春联
    
    text        - 春联内容,以空格断行
    HorV        - H-横排,V-竖排
    quality     - 单字分辨率,H-640像素,M-480像素,L-320像素
    out_file    - 输出文件名
    """
    
    usize = 'H':(640,23), 'M':(480,18), 'L':(320,12)
    bg_im = get_bg(quality)
    text_list = [list(item) for item in text.split()]
    rows = len(text_list)
    cols = max([len(item) for item in text_list])
    
    if HorV == 'V':
        ow, oh = 40+rows*usize[quality][0]+(rows-1)*10, 40+cols*usize[quality][0]
    else:
        ow, oh = 40+cols*usize[quality][0], 40+rows*usize[quality][0]+(rows-1)*10
    out_im = Image.new('RGBA', (ow, oh), '#f0f0f0')
    
    for row in range(rows):
        if HorV == 'V':
            row_im = Image.new('RGBA', (usize[quality][0], cols*usize[quality][0]), 'white')
            offset = (ow-(usize[quality][0]+10)*(row+1)-10, 20)
        else:
            row_im = Image.new('RGBA', (cols*usize[quality][0], usize[quality][0]), 'white')
            offset = (20, 20+(usize[quality][0]+10)*row)
        
        for col, ch in enumerate(text_list[row]):
            if HorV == 'V':
                pos = (0, col*usize[quality][0])
            else:
                pos = (col*usize[quality][0],0)
            
            ch_im = get_word(ch, quality)
            row_im.paste(bg_im, pos)
            row_im.paste(ch_im, (pos[0]+usize[quality][1], pos[1]+usize[quality][1]), mask=ch_im)
            
        out_im.paste(row_im, offset)
    
    if out_file:
        out_im.convert('RGB').save(out_file)
    out_im.show()
if __name__ == "__main__":
 text1 = '普天同庆 欢度春节'
# text2 = '新年快乐'
 write_couplets(text1, HorV='V', quality='M', out_file='文宇肃然.jpg')
 # write_couplets(text2, HorV='V', quality='M', out_file='横批.jpg')

算算你的2022个人运势

我已经有点神棍的潜质了,各位图个乐就好

clear
clc
year=input('输入阳历年:\\n');
month=input('输入阳历月份:\\n');
day=input('输入阳历日期:\\n');
time0=input('输入时辰:\\n');
fen0=input('输入时分:\\n');
jieqi=input('输入时间所在节气:\\n','s');
%year=2021;month=8;day=27;time0=21;fen0=28;jieqi='处暑';
[sizhuzhuanhuanjieguo,dun,jushu]=qimendunjia(year,month,day,time0,fen0,jieqi)


function [sizhuzhuanhuanjieguo,dun,jushu]=qimendunjia(year,month,day,time0,fen0,jieqi)
tiangan='甲乙丙丁戊己庚辛壬癸甲乙丙丁戊己庚辛壬癸';%十天干双排矩阵,双排是为了排布天盘干和天盘干是转排使用
dizhi='子丑寅卯辰巳午未申酉戌亥子丑寅卯辰巳';%十二地支矩阵,为了确定四柱地支
%确定年柱(注意:阳历年立春之前和立春之后的计算是不一样的)
lichunflag=(month==1)|(month==2&day<4);
if (lichunflag~=1)
    y=mod((year-3),10);
    if(y==0)
       nianganshu=10;
    else
       nianganshu=y;
    end
               %用公式确定年干
else
    y=mod((year-1-3),10);%立春之前
    if(y==0)
       nianganshu=10;
    else
       nianganshu=y;
    end
end

if (lichunflag~=1)
    z=mod((year-3),12);
    if(z==0)
       nianzhishu=12;
    else
       nianzhishu=z;
    end                      %用公式确定年支
else
    z=mod((year-1-3),12); %立春之前
    if(z==0)
       nianzhishu=12;
    else
       nianzhishu=z;
    end  
end

niangan=tiangan(nianganshu);%呈现年干字符
nianzhi=dizhi(nianzhishu);%呈现年支字符


%确定月柱

   if(nianganshu==1|nianganshu==6)
      k=3;
   else if(nianganshu==2|nianganshu==7)
        k=5;
        else if(nianganshu==3|nianganshu==8)
            k=7;
             else if(nianganshu==4|nianganshu==9)
                k=9;
                  else if(nianganshu==5|nianganshu==10)
                    k=1;
                      end
                 end
            end
        end
    end  %以上使用五虎遁法确定正月起始天干位置
if(lichunflag~=1) 
   yueganshu=k-1+month-1;%从正月开始数,确定所测月份的月干数
   yuezhishu=2+month-1; %正月建寅,所以从寅月开始数,确定所测月份的月支数
else
    yueganshu=k-1+12;
    yuezhishu=2;
end
yuegan=tiangan(yueganshu);%呈现月干字符
yuezhi=dizhi(yuezhishu);%呈现月支字符
%确定日柱
if(mod(month,2)==0)
    M=30;
else
    M=0;
end
flag=(mod(year,4)==0&mod(year,100)~=0)|mod(year,400)==0;%确定是否为闰年
if(flag==0)
if (month==1|month==4|month==5)
    shengyushu=1;
else if(month==2|month==6|month==7)
        shengyushu=2;
    else if(month==3)
            shengyushu=0;
        else if(month==8)
                shengyushu=3;
            else if(month==9|month==10)
                    shengyushu=4;
                else if(month==11|month==12)
                        shengyushu=5;
                    end
                end
            end
        end
    end
end
else 
    if (month==2|month==4|month==5)
    shengyushu=1;
else if(month==6|month==7)
        shengyushu=2;
    else if(month==3|month==1)
            shengyushu=0;
        else if(month==8)
                shengyushu=3;
            else if(month==9|month==10)
                    shengyushu=4;
                else if(month==11|month==12)
                        shengyushu=5;
                    end
                end
            end
        end
    end
    end
end
G=(year-1900)*5+floor((year-1900)/4)+9+M+day+shengyushu;%日干支计算重要公式
riganshu0=mod(mod(G,60),10);
rizhishu0=mod(mod(G,60),12);
if(riganshu0==0)
    riganshu=10;
else riganshu=riganshu0;
end
if(rizhishu0==0)
    rizhishu=12;
else
    rizhishu=rizhishu0;
end
rigan=tiangan(riganshu);
rizhi=dizhi(rizhishu);
%确定时柱
fen=fen0/100;
time=time0+fen;
if((time>=0&&time<1)|(time>=23&&time<24))
    shizhishu=1;
    p=1;
else if(time>=1&&time<3)
    shizhishu=2;
    p=2;
   else if(time>=3&&time<5)
    shizhishu=3;
    p=3; 
    else if(time>=5&&time<7)
    shizhishu=4;
    p=4;
    else if(time>=7&&time<9)
    shizhishu=5;
    p=5;
    else if(time>=9&&time<11)
    shizhishu=6;
    p=6;
    else if(time>=11&&time<13)
    shizhishu=7;
    p=7;
    else if(time>=13&&time<15)
    shizhishu=8;
    p=8;
    else if(time>=15&&time<17)
    shizhishu=9;
    p=9;
    else if(time>=17&&time<19)
    shizhishu=10;
    p=10;
    else if(time>=19&&time<21)
    shizhishu=11;
    p=11;
    else shizhishu=12;p=12;
        end
        end
        end
        end
        end
        end
        end
        end
        end
       end
end     %以上先确定时辰对应地支

if (riganshu==1|riganshu==6)
    q=1;
else if(riganshu==2|riganshu==7)
        q=3;
    else if(riganshu==3|riganshu==8)
            q=5;
        else if(riganshu==4|riganshu==9)
                q=7;
            else q=9;
            end
        end
    end
end  %以上使用五鼠遁来确定该日子时的天干排位
shiganshu=q-1+p; %从子时开始朝后数,直到到规定时辰,确定时干
shigan=tiangan(shiganshu); %呈现时干字符
shizhi=dizhi(shizhishu);%呈现时支字符
%输出结果
fprintf('阳历%d年%d月%d日%d时%d分对应的四柱八字如下:\\n',year,month,day,time0,fen0);
sizhuzhuanhuanjieguo=[niangan,nianzhi,'年',yuegan,yuezhi,'月',rigan,rizhi,'日',shigan,shizhi,'时'];
disp(sizhuzhuanhuanjieguo);
fprintf('----------------------------------------------------------------------------------------\\n');
 %确定阴遁阳遁与局数
 if ((month>=7&month<=11)|(month==6&day>=21)|(month==12&day<21)) %夏至一阴生
     dun='阴遁';
 else
     dun='阳遁'; %冬至一阳生
 end
 jieqijuzhen=['立春';'雨水';'惊蛰';'春分';'清明';'谷雨';'立夏';'小满';'芒种';'夏至';'小暑';'大暑';
     '立秋';'处暑';'白露';'秋分';'寒露';'霜降';'立冬';'小雪';'大雪';'冬至';'小寒';'大寒'];%由节气确定局数
 jieqi1=jieqi(1);jieqi2=jieqi(2);
for i=1:24
    if (jieqijuzhen(i,1)==jieqi1&jieqijuzhen(i,2)==jieqi2)
        m=i;
    end
end
dingyuanjuzhen='甲子','乙丑','丙寅','丁卯','戊辰';
                '己巳','庚午','辛未','壬申','癸酉';
                '甲戌','乙亥','丙子','丁丑','戊寅';
                '己卯','庚辰','辛巳','壬午','癸未';
                '甲申','乙酉','丙戌','丁亥','戊子';
                '己丑','庚寅','辛卯','壬辰','癸巳';
               '甲午','乙未','丙申','丁酉','戊戌';
                '己亥','庚子','辛丑','壬寅','癸卯';
               '甲辰','乙巳','丙午','丁未','戊申';
                '己酉','庚戌','辛亥','壬子','癸丑';
               '甲寅','乙卯','丙辰','丁巳','戊午';
                '己未','庚申','辛酉','壬戌','癸亥'
                ;
for i=1:12
    for j=1:5
        if (dingyuanjuzheni,j==[rigan,rizhi])%由日柱确定三元
            k=i;
        end
    end
end
if(mod(k,3)==1)
    n=1;
else if(mod(k,3)==2)
        n=2;
    else n=3;
    end
end
jushujuzhen=[8 5 2;9 6 3;1 7 4;3 9 6;4 1 7;5 2 8;
             4 1 7;5 2 8;6 3 9;9 3 6;8 2 5;7 1 4;
             2 5 8;1 4 7;9 3 6;7 1 4;6 9 3;5 8 2;
             6 9 3;5 8 2;4 7 1;1 7 4;2 8 5;3 9 6];
jushu=jushujuzhen(m,n);%第m个节气的第n元,对应的局数
fprintf('阴阳遁局结果如下:\\n');
disp([dun,num2str(jushu),'局',jieqi]);
fprintf('----------------------------------------------------------------------------------------\\n');
%确定地盘九宫落位
if (jushu==5&dun=='阳遁')
    dipangan='乙戊己庚辛壬癸丁丙'; %确定局数为5,戊落中五宫,寄于坤二宫。阳遁顺排
else if (jushu==5&dun=='阴遁')
        dipangan='已戊乙丙丁癸壬辛庚'; %确定局数为5,戊落中五宫,寄于坤二宫。阴遁逆排
    else       
yiweizifu1=[];
if (dun=='阳遁')
    shunpaitiangan='戊己庚辛壬癸丁丙乙';
    yiweizifushu=jushu-1; %第几局戊就要落在哪一宫,之后按照宫格数字从大到小排布,
                          %若想得到对应123456789的天干排布,需要将末尾一些字符提到前面
    for i=10-yiweizifushu:9
        yiweizifu1=[yiweizifu1,shunpaitiangan(i)];%得到的yiweizifu1代表需要提到前面的字符
    end
    dipangan0=[yiweizifu1,shunpaitiangan];%将提到前面的字符与shunpaitiangan重新组合
    dipangan=dipangan0(1,1:9);%得到一个底盘干对应宫格排位
else
    shunpaitiangan='戊己庚辛壬癸丁丙乙'; 
    %yiweizifushu=9-jushu; %由于是阴遁,需逆排,是从局数从大到小数,需要移位的字符数变为9-局数。
    for i=jushu+1:9
        yiweizifu1=[yiweizifu1,shunpaitiangan(i)];
    end
    dipangan0=[yiweizifu1,shunpaitiangan];
    for i=1:9
        dipangan(i)=dipangan0(10-i);%逆宫位排布转换为顺排
    end
end
    end
end
fprintf('地盘九宫落位如下:\\n');
disp(dipangan);
fprintf('----------------------------------------------------------------------------------------\\n');
%排布天盘干
%先确定旬首
liushihuajiazibiao='甲子','乙丑','丙寅','丁卯','戊辰','己巳','庚午','辛未','壬申','癸酉';
                    '甲戌','乙亥','丙子','丁丑','戊寅','己卯','庚辰','辛巳','壬午','癸未';
                    '甲申','乙酉','丙戌','丁亥','戊子','己丑','庚寅','辛卯','壬辰','癸巳';
                    '甲午','乙未','丙申','丁酉','戊戌','己亥','庚子','辛丑','壬寅','癸卯';
                    '甲辰','乙巳','丙午','丁未','戊申','己酉','庚戌','辛亥','壬子','癸丑';
                    '甲寅','乙卯','丙辰','丁巳','戊午','己未','庚申','辛酉','壬戌','癸亥';
for i=1:6
    for j=1:10
        if (liushihuajiazibiaoi,j==[shigan,shizhi])%寻找时柱在六十花甲子表中的位置,以此确定旬首
            k0=i;
            kongwang0=i;%确定空亡 
            zhishimen0=j;%值使门移位数
        end
    end
end


xunshou0=liushihuajiazibiaok0,1;%提取旬首前两个字符
if (xunshou0=='甲子')
    xunshou='甲子戊';
else if (xunshou0=='甲戌')
        xunshou='甲戌己';
    else if (xunshou0=='甲申')
            xunshou='甲申庚';
        else if (xunshou0=='甲午')
                xunshou='甲午辛';
            else if (xunshou0=='甲辰')
                    xunshou='甲辰壬';
                else xunshou='甲寅癸';
                end
            end
        end
    end
end %以上选择语句确定完整旬首
fprintf('时柱确定旬首为:\\n')
disp(xunshou);
fprintf('----------------------------------------------------------------------------------------\\n');
zhuanpai=[dipangan(1),dipangan(8),dipangan(3),dipangan(4),dipangan(9),dipangan(2),dipangan(7),dipangan(6)];%由于转宫与宫格数的位置
 tianpanxunshouluowei=find(zhuanpai==shigan)  ;                                                                                                        %不对应,需要调整为宫位循环排位
jiedianshu=find(zhuanpai==xunshou(3));
yiweizifu2=[];
for i=1:jiedianshu-1  %旬首之前的移到后面
    yiweizifu2=[yiweizifu2,zhuanpai(i)];
end
zhuanpai1=[zhuanpai,yiweizifu2];
zhuanpai2=zhuanpai1(jiedianshu:length(zhuanpai1));%确定旬首第三字符为首的天干排位
yiweishu=tianpanxunshouluowei-1;
yiweizifu3=[];
for i=9-yiweishu:8 %按照18349276的宫位顺序排布zhuanpai2
    yiweizifu3=[yiweizifu3,zhuanpai2(i)];
end
tianpanzhuanpai0=[yiweizifu3,zhuanpai2];
tianpanzhuanpai0;
tianpanzhuanpai=tianpanzhuanpai0(1:8);%取前八位,即为18349276宫对应天盘干排序
tianpangan=[tianpanzhuanpai(1),tianpanzhuanpai(6),tianpanzhuanpai(3),tianpanzhuanpai(4),dipangan(5),tianpanzhuanpai(8),tianpanzhuanpai(7),tianpanzhuanpai(2),tianpanzhuanpai(5)];
%调整为按宫位大小排序
fprintf('天盘干九宫落位如下:\\n')
disp(tianpangan);
fprintf('----------------------------------------------------------------------------------------\\n');
%排布九星
yuandangong=find(dipangan==xunshou(3));%确定地盘干中旬首落宫,即为大值符和值使门所落元旦宫
if (yuandangong==1)
    dazhifu='天蓬星';
    zhishimen='休门';
else if (yuandangong==2)
        dazhifu='天芮星';
         zhishimen='死门';
    else if (yuandangong==3)
            dazhifu='天冲星';
             zhishimen='伤门';
        else if (yuandangong==4)
                dazhifu='天辅星';
                 zhishimen='杜门';
            else if (yuandangong==6)
                    dazhifu='天心星';
                     zhishimen='开门';
                else if (yuandangong==7)
                        dazhifu='天柱星';
                         zhishimen='惊门';
                    else if (yuandangong==8)
                            dazhifu='天任星'
                             zhishimen='生门';
                        else dazhifu='天英星'; zhishimen='景门';
                        end
                    end
                end
            end
        end
    end
end %提取大值符和值使门
fprintf('大值符确认如下:\\n')
disp(dazhifu);
fprintf('----------------------------------------------------------------------------------------\\n');
fprintf('值使门确认如下:\\n')
disp(zhishimen);
fprintf('----------------------------------------------------------------------------------------\\n');
quanbujiuxing='天蓬星','天任星','天冲星','天辅星','天英星','天芮星','天柱星','天心星','天禽星';
jiuxingjuzhen='天蓬星','天任星','天冲星','天辅星','天英星','天芮星','天柱星','天心星';
tianpanxunshou=find(tianpangan==xunshou(3));%找到天盘旬首所落宫位,此处也是大小值符落宫所在。
%tianpanxunshou;
for i=1:8
    if (jiuxingjuzheni==dazhifu)%寻找大值符在九星矩阵中的位置
        k=i;
    end
end
yiweiyuansu=;
for i=1:k-1  %k==0
    yiweiyuansu=[yiweiyuansu,jiuxingjuzheni];%将大值符前的字符确定为移位字符,并提取,放在后面
end
jiuxingjuzhen1=[jiuxingjuzhen,yiweiyuansu];
%jiuxingjuzhen1,
jiuxingjuzhen2=;
for i=k:length(jiuxingjuzhen1)
jiuxingjuzhen2=[jiuxingjuzhen2,jiuxingjuzhen1i];
%将大值符移到首位,得到新的九星矩阵2
end
%jiuxingjuzhen2,
if (tianpanxunshou==1)
    kk=1;
else if (tianpanxunshou==2)
        kk=6;
    else if (tianpanxunshou==3)
            kk=3;
        else if (tianpanxunshou==4)
                kk=4;
            else if (tianpanxunshou==6)
                kk=8;
                else if (tianpanxunshou==7)
                        kk=7;
                    else if (tianpanxunshou==8)
                            kk=2;
                        else kk=5;
                        end
                    end
                end
            end
        end
    end
end %天盘旬首落宫即是大值符落宫,以上是协调宫位与宫格循环之间不同,将落宫数字转换成循环宫位的顺序
yiweiyuansushu=kk-1; %大值符落在循环宫位的第kk个,则有末尾kk-1个需补到循环宫位前kk-1个。
yiweiyuansu2=;
for i=9-yiweiyuansushu:8
    yiweiyuansu2=[yiweiyuansu2,jiuxingjuzhen2i];%得到移动的字符
end
jiuxing0=[yiweiyuansu2,jiuxingjuzhen2];%移位的连缀九星矩阵2
%jiuxing0,
jiuxingluogong=;
for i=1:8
    jiuxingluogong=[jiuxingluogong,jiuxing0i];%得到九星按18349276宫位排布的结果。
end
jiuxing=jiuxingluogong1,jiuxingluogong6,jiuxingluogong3,jiuxingluogong4,quanbujiuxing9,jiuxingluogong8,jiuxingluogong7,jiuxingluogong2,jiuxingluogong5;%转换为宫格排序
fprintf('九星落宫结果为:\\n')%注九星不论阴阳遁,皆顺排。
disp(jiuxing);
fprintf('----------------------------------------------------------------------------------------\\n');
%排布八神
bashenjuzhen='值符','腾蛇','太阴','六合','白虎','玄武','九地','九天','值符','腾蛇','太阴','六合','白虎','玄武','九地','九天','中宫';
if (dun=='阳遁')
    gongge=[1 8 3 4 9 2 7 6];
    k=find(gongge==tianpanxunshou);%寻找循环宫位中天盘旬首落宫顺序
    qidian=9-(k-1);%比如说值符落在第k个宫,则从八神矩阵中中间前推k-1位作为起点。
    jiedian=qidian+7;%终点
    bashen0=;
    for i=qidian:jiedian
        bashen0=[bashen0,bashenjuzheni];%得到按照18349276排布的八神。
    end
    bashen=bashen01,bashen06,bashen03,bashen04,bashenjuzhen17,bashen08,bashen07,bashen02,bashen05;%转换为123456789排位的。
end
 if (dun=='阴遁')
    gongge=[1 6 7 2 9 4 3 8];
    k=find(gongge==tianpanxunshou);
    qidian=9-(k-1);
    jiedian=qidian+7;
    bashen0=;
    for i=qidian:jiedian
        bashen0=[bashen0,bashenjuzheni];
    end
    bashen=bashen01,bashen04,bashen07,bashen06,bashenjuzhen17,bashen02,bashen03,bashen08,bashen05;
end         
fprintf('八神落宫结果如下:\\n');
disp(bashen);
fprintf('----------------------------------------------------------------------------------------\\n');
%排布八门
bamenjuzhen0='休门','生门','伤门','杜门','景门','死门','惊门','开门';
bamenjuzhen='休门','生门','伤门','杜门','景门','死门','惊门','开门','休门','生门','伤门','杜门','景门','死门','惊门','开门','中宫';
xunshoushiluogong=find(dipangan==xunshou(3));
if (dun=='阳遁')
    fuzhuzhuanmen=[1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9];
    k=find(fuzhuzhuanmen==xunshoushiluogong);%确定旬首落宫位置
    k1=k(1)+zhishimen0-1;%提取k的第一个元素,即为旬首落宫,之后再往后递推,直到时柱所在。
    k2=fuzhuzhuanmen(k1);%日柱时,值使门落宫数
    for i=1:8
        if(bamenjuzheni==zhishimen)
            m=i;%找到八门矩阵中值使门所在位置。
        end
    end
    bamen0=;
    for i=m:m+7
        bamen0=[bamen0,bamenjuzheni];%将值使门提到第一位,得到新的八门矩阵八门0。
    end
    bamen1=[bamen0,bamen0];%值使门在第一位时的八门双排状态
    gongge=[1 8 3 4 9 2 7 6];
    if (k2~=5)
    n=find(gongge==k2);%k2时是值使门落宫,看这个宫在18349276宫的顺序为n。
    qidian=9-(n-1);%从八门0中间前推n-1个字符位
    jiedian=qidian+7;%终点
    bamen2=;
    for i=qidian:jiedian
        bamen2=[bamen2,bamen1i];%得到按照18349276排布的八门。
    end
    else  k2=2;%值使门在时柱落在中五宫时,寄在坤2宫。
    n=find(gongge==k2);
    qidian=9-(n-1);
    jiedian=qidian+7;
    bamen2=;
    for i=qidian:jiedian
        bamen2=[bamen2,bamen1i];
    end
    end
    bamen=bamen21,bamen26,bamen23,bamen24,bamenjuzhen17,bamen28,bamen27,bamen22,bamen25;%调整为123456789八门分布。
end

 if (dun=='阴遁')
    fuzhuzhuanmen=[9 8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 1];%阴遁数值使门时柱落宫逆着数。
    k=find(fuzhuzhuanmen==xunshoushiluogong);
    k;
    k1=k(1)+zhishimen0-1;
    k2=fuzhuzhuanmen(k1);%日柱时,值使门落宫数
    for i=1:8%前八个字符中寻找到值使门,
        if(bamenjuzheni==zhishimen)
            m=i;
        end
    end
    bamen0=;
    for i=m:m+7
        bamen0=[bamen0,bamenjuzheni];%将值使门提到第一位
    end
    bamen1=[bamen0,bamen0];%值使门在第一位时的八门双排状态
    gongge=[1 8 3 4 9 2 7 6];
    if (k2~=5)
    n=find(gongge==k2);%确定时柱时,值使门落宫在循环排布时的位置。
    qidian=9-(n-1);%从中间前推n-1为,18349276排布的起点。
    jiedian=qidian+7;%终点
    bamen2=;
    for i=qidian:jiedian
        bamen2=[bamen2,bamen1i];%得到18349276排布的八门状态。
    end
    else  k2=2;%中宫寄在坤二宫
    n=find(gongge==k2);
    qidian=9-(n-1);
    jiedian=qidian+7;
    bamen2=;
    for i=qidian:jiedian
        bamen2=[bamen2,bamen1i];
    end
    end
    bamen=bamen21,bamen26,bamen23,bamen24,bamenjuzhen17,bamen28,bamen27,bamen22,bamen25; %调整12345678排布
end   
fprintf('八门落宫结果如下:\\n')
disp(bamen);
fprintf('----------------------------------------------------------------------------------------\\n');
%确定马星所在
if (rizhi=='申'|rizhi=='子'|rizhi=='辰')%申子辰马在寅
    text(10,35,'馬');maxing='寅';hold on;
else
    if (rizhi=='寅'|rizhi=='午'|rizhi=='戌')%寅午戌马在申
       text(80,115,'馬');maxing='申';hold on; 
    else 
        if (rizhi=='巳'|rizhi=='酉'|rizhi=='丑')%巳酉丑马在亥
       text(80,35,'馬');maxing='亥'; hold on; 
        else   text(20,115,'馬');maxing='巳';hold on;%亥卯未马在巳
        end
    end
end
fprintf('马星落宫结果如下:\\n')
disp(maxing);
fprintf('----------------------------------------------------------------------------------------\\n');
%确定空亡所在
if (kongwang0==1)
    kongwang='戌亥';
    scatter(80,35,'ko');hold on;
else if (kongwang0==2)
      kongwang='申酉';  
      scatter(80,115,'ko');hold on;
      scatter(80,75,'ko');hold on;
    else if (kongwang0==3)
      kongwang='午未';  
      scatter(50,115,'ko');hold on;
      scatter(70,115,'ko');hold on;
        else if (kongwang0==4)
      kongwang='辰巳';  
      scatter(10,115,'ko');hold on;
            else if (kongwang0==5)
      kongwang='寅卯';  
      scatter(10,35,'ko');hold on;
      scatter(10,75,'ko');hold on;
                else 
      kongwang='子丑';  
      scatter(40,35,'ko');hold on;
      scatter(10,35,'ko');hold on;
                end
            end
        end
    end
end
fprintf('空亡确定如下:\\n')
disp(kongwang);
fprintf('----------------------------------------------------------------------------------------\\n');
 %可视化程序代码
 jiugongming='坎坤震巽中乾兑艮离';
 %首先画好九宫格
 x1=linspace(0,90,500);
 y1=linspace(0,0,500);
 plot(x1,y1,'k-','linewidth',1)
 hold on;
 y2=linspace(40,40,500);
 plot(x1,y2,'k-','linewidth',1)
 hold on;
 y3=linspace(80,80,500);
 plot(x1,y3,'k-','linewidth',1)
 hold on;
 y4=linspace(120,120,500);
 plot(x1,y4,'k-','linewidth',1)
 hold on;
 
 x2=linspace(0,0,500);
 z=linspace(0,120,500);
 plot(x2,z,'k-','linewidth',1);
 hold on;
 x3=linspace(30,30,500);
 z=linspace(0,120,500);
 plot(x3,z,'k-','linewidth',1);
 hold on;
 x4=linspace(60,60,500);
 z=linspace(0,120,500);
 plot(x4,z,'k-','linewidth',1);
 hold on;
 x5=linspace(90,90,500);
 z=linspace(0,120,500);
 plot(x5,z,'k-','linewidth',1);
 hold on;
 %再标注各个宫位天地人盘
 %兑一宫
 k=1;
 text(40,10,bamenk);hold on;
 text(40,20,jiuxingk);hold on;
 text(40,30,bashenk);hold on;
 text(50,10,dipangan(k));hold on;
 text(50,20,tianpangan(k));hold on;
 text(50,30,[jiugongming(k),num2str(k)]);hold on;
 %坤二宫
 k=2;
 text(70,90,bamenk);hold on;
 text(70,100,jiuxingk);hold on;
 text(70,110,bashenk);hold on;
 text(80,90,dipangan(k));hold on;
 text(80,100,tianpangan(k));hold on;
 text(80,110,[jiugongming(k),num2str(k)]);hold on;
 %震三宫
 k=3;
 text(10,50,bamenk);hold on;
 text(10,60,jiuxingk);hold on;
 text(10,70,bashenk);hold on;
 text(20,50,dipangan(k));hold on;
 text(20,60,tianpangan(k));hold on;
 text(20,70,[jiugongming(k),num2str(k)]);hold on;
 %巽四宫
 k=4;
 text(10,90,bamenk);hold on;
 text(10,100,jiuxingk);hold on;
 text(10,110,bashenk);hold on;
 text(20,90,dipangan(k));hold on;
 text(20,100,tianpangan(k));hold on;
 text(20,110,[jiugongming(k),num2str(k)]);hold on;
 %中五宫
 text(36,70,[niangan;nianzhi;'年']);hold on;
 text(42,70,[yuegan;yuezhi;'月']);hold on;
 text(48,70,[rigan;rizhi;'日']);hold on;
 text(54,70,[shigan;shizhi;'时']);hold on;
 %乾六宫
 k=6;
 text(70,10,bamenk);hold on;
 text(70,20,jiuxingk);hold on;
 text(70,30,bashenk);hold on;
 text(80,10,dipangan(k));hold on;
 text(80,20,tianpangan(k));hold on;
 text(80,30,[jiugongming(k),num2str(k)]);hold on;
 %兑七宫
 k=7;
 text(70,50,bamenk);hold on;
 text(70,60,jiuxingk);hold on;
 text(70,70,bashenk);hold on;
 text(80,50,dipangan(k));hold on;
 text(80,60,tianpangan(k));hold on;
 text(80,70,[jiugongming(k),num2str(k)]);hold on;
 %艮八宫
 k=8;
 text(10,10,bamenk);hold on;
 text(10,20,jiuxingk);hold on;
 text(10,30,bashenk);hold on;
 text(20,10,dipangan(k));hold on;
 text(20,20,tianpangan(k));hold on;
 text(20,30,[jiugongming(k),num2str(k)]);hold on;
 %离九宫
 k=9;
 text(40,90,bamenk);hold on;
 text(40,100,jiuxingk);hold on;
 text(40,110,bashenk);hold on;
 text(50,90,dipangan(k));hold on;
 text(50,100,tianpangan(k));hold on;
 text(50,110,[jiugongming(k),num2str(k)]);hold on;
 title(['起局时间:',num2str(year),'年',num2str(month),'月',num2str(day),'日',num2str(time0),'时',num2str(fen0),'分']);
end

后记

回答个粉丝关心的问题:

博主你好,你的专栏会停更吗?

只要CSDN不倒,专栏更新不停,这是一个不断学习的过程,技术总是在不断更新,我们也在不断的学习。总会有新的学习感悟,对不对。所有专栏都会不断做更新,希望能和大家一起学习,一起进步!

本文最后,我会着重介绍我的付费专栏,也正是写付费专栏,让我一直坚持更新博客,因为付费,如果不更新,会有付费玩家催更,为了他们,你不得不更新,一直良性循环下去。

贴上专栏的介绍

【专栏导读】https://wenyusuran.blog.csdn.net/article/details/121031338?spm=1001.2014.3001.5502

虎年画老虎和年兽出没的代码正在构思中,至于几时能完成,我也不知道,哈哈哈!

以上是关于MATLAB应用实战系列(101)-新年新祝福,教你如何用MATLAB以对联的形式打开2022年,爱满人间(附MATLAB和Python代码实现)的主要内容,如果未能解决你的问题,请参考以下文章

数学建模MATLAB应用实战系列(九十)-TOPSIS法应用案例(附MATLAB和Python代码)

数学建模MATLAB应用实战系列(九十)-变异系数法应用案例(附MATLAB和Python代码)

经典温馨新年祝福朋友圈话语

来自AI浩的新年祝福

来自AI浩的新年祝福

Python3,我把新年祝福写在“雨“中,你看,雨一直下,气氛还算融洽,在同个屋檐下....