Hello everyone. How to make a matrix with unequal number of rows and columns, and make the number of rows and columns the same by adding zeros. Thank you very much.
显示 更早的评论
How to make a matrix with unequal number of rows and columns, and make the number of rows and columns the same by adding zeros.
采纳的回答
更多回答(2 个)
Image Analyst
2021-5-31
Try this:
a = rand(5, 2);
% Get longest dimension.
longestDimension = length(a)
% Make rows and columns of a both be longestDimension by adding zeros:
a(longestDimension, longestDimension) = 0 % Expand shortest dimension.
whos a % Show size in command window
Mathieu NOE
2021-5-31
hello
see example code below
clearvars;
load('dataA.mat')
[m,n] = size(dataA);
if m>=n
out = zeros(m,m);
out(1:m,1:n) = dataA;
else
out = zeros(n,n);
out(1:m,1:n) = dataA;
end
5 个评论
Image Analyst
2021-5-31
Evidently you've never seen the "trick" used in my answer.
Mathieu NOE
2021-5-31
sorry but according to what I can see, your answer appeared after mine , I admit by only a difference of a few minutes
but I wil not fight here on this very basic question, I have other priorities
Image Analyst
2021-5-31
Mine was 22 minutes ago while yours was only 17 minutes ago, but no big deal. That often happens when people are working on the question at the same time - no big deal. Just thought you and others would like to know of this trick, like I was years ago when I learned the trick from Walter. I use it often now. And of course it's always good to see alternative ways of solving the same problem. For example some people like a longer, well commented solution with longer but descriptive variable names, while others prefer to use a shorter but much more cryptic one liner like you might get with regexp(), bsxfun(), or similar functions. Anyway, have a good day. 😃
Rik
2021-5-31
Is that comment about cryptic code a jab at my solution? 😉
Image Analyst
2021-5-31
Ha - no Rik. Actually last time I looked there were only two Answers - yours was not there. Actually I admire people who know the cryptic ways of doing things because they're often something I cannot do or understand. It's just that often/usually I give code that looks longer because I have long descriptive variable names and lots of comments and I usually lose out to those who come along with a one liner of alphabet soup because the poster liked that it was very short. However they often come back with something like "well it works but I don't understand it so can you explain it?".
类别
在 帮助中心 和 File Exchange 中查找有关 Spline Postprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!