Section an array into equal rows and changing simultaneously the values of each section.
Mostra commenti meno recenti
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
dpb
il 3 Ago 2020
Give us an example input and expected output (and rules to get from one to the other).
Princewill Azorom
il 3 Ago 2020
dpb
il 3 Ago 2020
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
>>
Princewill Azorom
il 3 Ago 2020
dpb
il 3 Ago 2020
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.
Risposte (1)
Bruno Luong
il 3 Ago 2020
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
Princewill Azorom
il 4 Ago 2020
Bruno Luong
il 4 Ago 2020
>> 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
dpb
il 4 Ago 2020
>> 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
>>
Princewill Azorom
il 5 Ago 2020
Modificato: dpb
il 5 Ago 2020
dpb
il 5 Ago 2020
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.
Bruno Luong
il 5 Ago 2020
Modificato: Bruno Luong
il 5 Ago 2020
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
Steven Lord
il 5 Ago 2020
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.
Princewill Azorom
il 6 Ago 2020
dpb
il 6 Ago 2020
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
Princewill Azorom
il 7 Ago 2020
Categorie
Scopri di più su Matrices and Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!