how to combain matrix with vector
Info
This question is closed. Reopen it to edit or answer.
Show older comments
if I have matrix a = [9 8 7; 6 5 4] and vector v = [1 2 3 4 5] I want to combaine matrix like this
[9 8 7 inf inf; ...
6 5 4 inf inf; ...
1 2 3 4 5]
thank you!!
1 Comment
Birdman
on 12 Dec 2017
What have you done so far?
Answers (3)
Something like this?
a = [9 8 7; 6 5 4];
v = [1 2 3 4 5];
sz = size(a);
a = [a inf(sz(1),size(v,2)-sz(2));v]
4 Comments
Birdman
on 12 Dec 2017
Please do not provide direct solutions before seeing any effort from the user.
Well, you are right, Birdman. Actually nadav should show, what he has tried so far. But he has asked a specific question and did not post a copy of a homework assignment. He focused on the actual problem and provided a clear definition of the inputs and outputs. I'm rather sure, that this is not a homework problem at all. I trust KL's and Andrei's estimation, that this is not a "homework-cheating", but to be true: It is hard to define exactly, where the border is.
I personally decide this by the following method: I imagine that the asking person is one of my students and the problem concerns a homework I have given. When a student shows me a printout of the question, I will not help him or her. But as soon as the student took the time to narrow down the problem, explain the wanted procedure and ask a specific question, I try to assist or provide a full solution, if this is more efficient.
The vast majority of question could be solved by the asking persons with a certain effort. The forum does not know any magic secrets. But sometimes it is more efficient to use the experiences of others. Sometimes a short and concise question means, that it is a "do it 4 me", but sometimes it shows, that the author tries to focus on the required facts to make it as easy as possible to answer.
I agree with Jan.
@Birdman: Usually when you see a homework, doit4me tag (tagged by volunteers), you could choose not to answer or you could give them some hints rather than explicit answers.
For example the one on the below link was clearly a copy-paste of homework question, a typical doit4me.
Steven Lord
on 12 Dec 2017
And if it sounds like it may be homework but you're not certain, you can gently nudge the poster into offering more information. I tend to use something like "If you show us what you've already tried we may be able to offer some suggestions for how to proceed." to do that nudging.
Andrei Bobrov
on 12 Dec 2017
Edited: Andrei Bobrov
on 12 Dec 2017
a = [9 8 7; 6 5 4];
v = [1 2 3 4 5];
z = {a;v};
[ii,jj] = cellfun(@size,z);
n = max(jj);
out = cell2mat(arrayfun(@(x,y,k)[z{x},inf(k,n - y)],(1:numel(z))',jj,ii,'un',0))
or
a = [9 8 7; 6 5 4];
v = [1 2 3 4 5];
z = {a;v};
m = numel(z);
[ii,jj] = cellfun(@size,z);
n = max(jj);
i2 = cumsum(ii);
i1 = i2 - ii + 1;
out = inf(sum(ii),n);
for ii = 1:m
out(i1(ii):i2(ii),1:jj(ii)) = z{ii};
end
1 Comment
Birdman
on 12 Dec 2017
Please do not provide direct solutions before seeing any effort from the user.
a = [9 8 7; 6 5 4];
v = [1 2 3 4 5];
out = padcat(a(1, :), a(2, :), v);
out(isnan(out)) = Inf;
Or write a function by your own:
% UNTESTED !!!
function Out = CatPadding(Dim, varargin)
In = varargin;
S1 = cellfun('size', In, 1);
S2 = cellfun('size', In, 2);
c = 1;
if Dim == 1
Out = Inf(sum(S1), max(S2));
for k = 1:numel(In)
Out(c:c+S1(k)-1, 1:S2(k)) = In{k};
c = c + S1(k);
end
elseif Dim == 2
Out = Inf(max(S1), sum(S2));
for k = 1:numel(In)
Out(1:S1(k), c:c+S2(k)-1) = In{k};
c = c + S2(k);
end
else
error('Only Dim=1 and Dim=2 implemented yet.')
end
And:
a = [9 8 7; 6 5 4];
v = [1 2 3 4 5];
Out = CatPadding(1, a, v)
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!