Section an array into equal rows and changing simultaneously the values of each section.

I've been trying to section a matrix into equal rows while changing simultaneously the values of each section but I've been stuck for a while now please I need help. Thanks.

5 Commenti

Give us an example input and expected output (and rules to get from one to the other).
I'm expected to produce an n by m matrix say 6 by 4 and that the first third (first two rows) produces the same numbers all through. Same with the second and third.
As said, show us an example of what you think is a valid solution...not clear precisely what you mean. The first Q? and the followup don't seem to have much to do with each other.
One solution for the latter--
>> k=3; n=2; m=4;
>> kron([1:k].',ones(n,m))
ans =
1 1 1 1
1 1 1 1
2 2 2 2
2 2 2 2
3 3 3 3
3 3 3 3
>>
Thanks this is exactly how the solution should look like but if the matrix had entirely different elements in each row how exactly am I supposed to make it have this outcome? And then when I change the dimension of the matrix I should still have the same result too. Thanks
Read
doc kron
and experiment with the above...the vector argument to kron() is what gets duplicated; I just used 1:k here; it can be any content of the right size.
The other comments don't make sense -- again, SHOW US WHAT YOU HAVE TO START WITH and then WHAT YOU THINK THE ANSWER SHOULD BE GIVEN THAT INPUT (and why).
Are you to create the array from scratch which the above does or is there some pre-existing array? If the former, you have the solution, all you have to do is decide what you want the tiled input to be.
If the latter, then the problem is as yet undefined because we don't have what the rule is on how to manipulate a starting set of data to what would be an acceptable final result.

Accedi per commentare.

Risposte (1)

Initial data
>> M=randi(10,3,4)
M =
1 3 9 4
6 10 3 4
8 8 5 6
Duplicate rows
>> repelem(M,2,1)
ans =
1 3 9 4
1 3 9 4
6 10 3 4
6 10 3 4
8 8 5 6
8 8 5 6

10 Commenti

Thanks but this is only a part to it. How do I silmutanously change the first third to a different element say(all 1's)middle third to say (all 4's)and bottom third to say(all 7's)
>> repelem([1; 4; 7],2,4)
ans =
1 1 1 1
1 1 1 1
4 4 4 4
4 4 4 4
7 7 7 7
7 7 7 7
>> kron([1 4 7].',ones(n,m))
ans =
1 1 1 1
1 1 1 1
4 4 4 4
4 4 4 4
7 7 7 7
7 7 7 7
>>
Okay so here is my code
function T = X(p,m)
n = 3*p;
T = Randi(5,n,m)
end
% I've been stuck here because I can't seem to get a correct line of code that successfully converts the top third of matrix T to a particular element same with the middle and the bottom third of the matrix T. The function should be able to take random input and still produce similar results. Thanks
Bruno and I have both shown code that does that -- you have to define the three-vector of values you want duplicated somehow...you've yet to say how the "particular element(s)" are to be defined.
It should be apparent what need for the line of code; a vector of the values to replicate and the sizes to which they should be made to match.
Replace the [1 4 7] above examples with whatever values you want T to contain...but only you can define what those should be.
function T = X(p,m)
v = randi(10,3,1);
T = repelem(v,p,m); % kron(v,ones(p,m));
end
Test it on Matlab command window
for k=1:5
X(2,6)
end
I seem to remember seeing a homework assignment like this posted on Answers in the past. If this is a homework assignment, please post the text of the assignment so we can see exactly what you're being asked to do.
It could be as simple as a call to repelem.
Okay... here goes. Write a function that takes two positive integer inputs n and m. The function returns a 3n by m matrix called T. The top third of T(an n by m submatrix) is all 1's, the middle third is all 2's while the bottom third is all 3's. Sorry for any inconveniences. Thanks for helping.
You've been given code already that does that -- all you need to do is wrap in a function per the description...homework is generally not solved on the forum by handing out complete solutions but by expecting poster to show work they've done and then receive hints on specific trouble spots...
Your example code above (that I copied below) doesn't fit the problem description -- there's nothing in the problem that says anything at all about there being any random component at all -- the function takes the two size argurments for the output array size and then returns a specifically constructed array of that size.
function T = X(p,m)
n = 3*p;
T = Randi(5,n,m)
end
Thank you this was helpful in figuring it out even tho it's just a part of the whole. I really do appreciate your efforts.

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by