필터 지우기
필터 지우기

Erorr in my code

조회 수: 2 (최근 30일)
Esra Demirbilek
Esra Demirbilek 2022년 3월 18일
댓글: Esra Demirbilek 2022년 3월 18일
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));

답변 (1개)

Image Analyst
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)
birincifaz = 3×5
8 1 1 2 3 8 8 9 3 5 1 1 9 1 3
newM = birincifaz.*(1:size(birincifaz,1))'
newM = 3×5
8 1 1 2 3 16 16 18 6 10 3 3 27 3 9
% Alternate way using a for loop
[rows, columns] = size(birincifaz)
rows = 3
columns = 5
newM = zeros(rows, columns);
for col = 1 : columns
for row = 1 : rows
newM(row, col) = birincifaz(row, col) * row;
end
end
newM
newM = 3×5
8 1 1 2 3 16 16 18 6 10 3 3 27 3 9
  댓글 수: 1
Esra Demirbilek
Esra Demirbilek 2022년 3월 18일
thank you so much

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by