Piecing a Puzzle together in Matlab
Show older comments
I was given a Matlab code that contained 8 pictures files that when combined make a larger picture. I created a matrix of zeros that each picture would go in but I am stuck on how to put the pictures into the matrix.
This is what I have so far:
% start fresh
clear all
close all
clc
%Viewing each piece of the puzzle
load('puzzle.mat')
% image(piece_1);
% image(piece_2);
% image(piece_3);
% image(piece_4);
% image(piece_5);
% image(piece_6);
% image(piece_7);
% image(piece_8);
% colormap(gray(256))
%declaring X
X = zeros(400,600);
%putting pieces of puzzle in X in order
%Displaying X
image(X);
I just do not know how to piece the images together in the matrix of zeros. Each piece is of a different length (EX. one piece is a 100X200 matrix where another is 100X250)
Answers (1)
You can concatenate two arrays horizontally and vertically by the cat() command:
x = rand(2, 2);
y = rand(2, 2);
h = cat(2, x, y) % equivalently: [x, y]
v = cat(1, x, y) % equivalently: [x; y]
So all you need is to concatenate the arrays horizontally at first, and vertically at second, perhaps:
[[piece_1, piece_2]; ...
[piece_3, piece_4 etc];
[etc.]]
or using the equivalent cat() commands.
Categories
Find more on Board games in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!