简单的MATLAB。

3 vues (au cours des 30 derniers jours)
xhwkfdu
xhwkfdu le 25 Mai 2023
小白求教
一个5X6阶矩阵,
A=[0,5,0,0,0,0;
0,0,5,5,5,0;
0,0,0,0,0,0;
0,0,0,0,0,0;
0,0,0,0,0,5];
for i=1:5%
for j=1:6%
if A(i,j)>0
M(i)=A(i,j);
end
end
end
想得到M=【5,5,5,5,5】
但是得到M=[5,5,0,0,5],请问哪里出了问题,怎么修改?谢谢

Réponse acceptée

yfgersch
yfgersch le 25 Mai 2023
A=[0,5,0,0,0,0;
0,0,5,5,5,0;
0,0,0,0,0,0;
0,0,0,0,0,0;
0,0,0,0,0,5];
for i=1:5%
for j=1:6%
if A(i,j)>0
M(j*i)=A(i,j);会遍历矩阵A所有元素
end
end
end
M(M==0)=[]
最简单的方式如下
A=[0,5,0,0,0,0;
0,0,5,5,5,0;
0,0,0,0,0,0;
0,0,0,0,0,0;
0,0,0,0,0,5];
M = A(A==5)'

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB 快速入门 dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!