Convert the mxn size Matrix to square Matrix without using loop or if
Show older comments
Hi,
I have a random mxn matrix, like 4x6 and I want it to be square. For example, a original 4x6 matrix would be created by deleting the first and second column to a formed a 4x4 matrix.
A =
0.8147 0.6324 0.9575 0.9572 0.4218 0.6557
0.9058 0.0975 0.9649 0.4854 0.9157 0.0357
0.1270 0.2785 0.1576 0.8003 0.7922 0.8491
0.9134 0.5469 0.9706 0.1419 0.9595 0.9340
B =
0.9575 0.9572 0.4218 0.6557
0.9649 0.4854 0.9157 0.0357
0.1576 0.8003 0.7922 0.8491
0.9706 0.1419 0.9595 0.9340
How can I create from A to B without using loop or if-statment? and it should be work for all mxn, which m and n could be any number.
I started like this but got stunned. [m n] = size(A);
newA(m,:) = [];
newA(:,n) = [];
Thanks
Accepted Answer
More Answers (1)
Image Analyst
on 11 Nov 2014
Try this:
clc;
A =[...
0.8147 0.6324 0.9575 0.9572 0.4218 0.6557
0.9058 0.0975 0.9649 0.4854 0.9157 0.0357
0.1270 0.2785 0.1576 0.8003 0.7922 0.8491
0.9134 0.5469 0.9706 0.1419 0.9595 0.9340]
[rows, columns] = size(A);
if rows > columns
% Delete rows. Take bottom-most rows
numRowsToDelete = rows - columns
newA = A(numRowsToDelete+1:end, :)
elseif rows < columns
% Delete columns
numColumnsToDelete = columns - rows
newA = A(:, numColumnsToDelete+1:end)
end
Categories
Find more on Creating and Concatenating Matrices 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!