Erorr in my code
1 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I am using Matlab 2015a. The code I wrote gives an error. How can I fix the error?
this is my code:
newM = birincifaz.*(1:size(birincifaz,1))';
newM(:,1)=[];
for k=1:size(newM,2)
r=unique(newM(:,k));
r=r(r~=0);
B(:,k)=r(randi(length(r),50,1));
end
----------
Error using .*
Matrix dimensions must agree.
Error in sonhal3 (line 2)
newM = birincifaz.*(1:size(birincifaz,1));
0 个评论
回答(1 个)
Image Analyst
2022-3-18
You're trying to multiply a 2-D matrix by a 1-D column vector that goes from 1 to the number of rows. This will not work in R2015a but will work in later versions where implicit expansion of the column vector to a 2-D matrix will occur. Please upgrade or else use a for loop to do the multiplication
% First way using implicit expansion
birincifaz = randi(9, 3, 5)
newM = birincifaz.*(1:size(birincifaz,1))'
% Alternate way using a for loop
[rows, columns] = size(birincifaz)
newM = zeros(rows, columns);
for col = 1 : columns
for row = 1 : rows
newM(row, col) = birincifaz(row, col) * row;
end
end
newM
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spline Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!