construct meshgrid matrices without using function meshgrid.

Hi, I am going to construct meshgrid matrices1 x(i, j), y(i, j), given the grid points x=[0: 0.1:10], y=[0:0.2:10]. when I try x.*y.'. It can't go through. I know I can produce an mn matrix C with elements C(i, j) = x(i)y(j), i=1:m, j= 1:n. But I dunno how can I start with.

 채택된 답변

Image Analyst
Image Analyst 2014년 12월 14일
Try this:
% Sample data.
x = [0 : 1 : 10] % Change the middle "step" number if you want.
y = [0 : 2 : 10]
% Traditional way using meshgrid().
[X, Y] = meshgrid(x, y)
% Alternate way using loop.
rows = length(y);
columns = length(x);
xLoop = zeros(rows, columns);
yLoop = zeros(rows, columns);
for column = 1 : length(x)
xLoop(:, column) = x(column);
yLoop(:, column) = y;
end
% Print to command window.
xLoop
yLoop

추가 답변 (1개)

Mert Yiyit
Mert Yiyit 2018년 12월 5일

0 개 추천

function [A,B] = mgrid(x,y)
m = length(x);
n = length(y);
for i= 1:n
for j= 1:m
A(i,j) = x(1,j);
end
end
for q = 1:m
for z = 1:n
B(z,q) = y(1,z);
end
end

댓글 수: 1

Stephen23
Stephen23 2018년 12월 5일
편집: Stephen23 2018년 12월 5일
Note that the outputs A and B should be preallocated, as Image Analyst's answer from four years ago shows.

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

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2014년 12월 14일

편집:

2018년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by