I get an error, what's wrong? on Sparse matrix logic and answer

This question is soft-locked: new answers that are equivalent to already posted answers may be deleted without prior notice. Please take the time to make sure your contributions add something new.
Write the function for
A sparse matrix is a large matrix with almost all elements of the same value (typically zero). The normal representation of a sparse matrix takes up lots of memory when the useful information can be captured with much less. A possible way to represent a sparse matrix is with a cell vector whose first element is a 2-element vector representing the size of the sparse matrix. The second element is a scalar specifying the default value of the sparse matrix. Each successive element of the cell vector is a 3-element vector representing one element of the sparse matrix that has a value other than the default. The three elements are the row index, the column index and the actual value. Write a function called "sparse2matrix" that takes a single input of a cell vector as defined above and returns the output argument called "matrix", the matrix in its traditional form. Consider the following run:
cellvec = {[2 3], 0, [1 2 3], [2 2 -3]};
matrix = sparse2matrix(cellvec)
matrix =
0 3 0
0 -3 0

댓글 수: 23

No question was asked.
Suraj Tawde
Suraj Tawde 2019년 4월 20일
편집: Suraj Tawde 2019년 4월 20일
The question is Write a function called sparse2matrix that takes a single input of a cell vector as defined above and returns the output argument called matrix, the matrix in its traditional form
You have been given a homework command. You have not asked a question. A question would be something like "What is a cell array in MATLAB?"
We are not going to give you the answer to your homework. You need to ask something specific, like "I wrote wrote the following code, but when I ran it I got 'subscripts must be logical values or positive integers' on line 4. What does that mean?"
you can try this, if you like it;
function matrix = sparse2matrix(cellvec)
A=cellvec{1};
b=cellvec{2};
mat(A(1),A(2))=b;
mat(:,:)=b;
s=size(cellvec);
for ii = 3:s(2)
mat(cellvec{ii}(1),cellvec{ii}(2))=cellvec{ii}(3);
end
matrix=mat;
A sparse matrix is a large matrix with almost all elements of the same value (typically zero). The normal representation of a sparse matrix takes up lots of memory when the useful information can be captured with much less. A possible way to represent a sparse matrix is with a cell vector whose first element is a 2-element vector representing the size of the sparse matrix. The second element is a scalar specifying the default value of the sparse matrix. Each successive element of the cell vector is a 3-element vector representing one element of the sparse matrix that has a value other than the default. The three elements are the row index, the column index and the actual value. Write a function called sparse2matrix that takes a single input of a cell vector as defined above and returns the output argument called matrix, the matrix in its traditional form. Consider the following run:
SOLUTION.
function matrix = sparse2matrix(cellvec)
matrix = cellvec{2} * ones(cellvec{1});
k = length(cellvec);
for i = 3:k
a= cellvec{i}(1:2);
d = a(1);
b = a(2);
c = cellvec{i}(3);
matrix(d,b) = c;
end
end
Thanks for the solution Aman. But i have just 1 query. If i write Line 2 as :
matrix = zeros(cellvec{1}(1),cellvec{1}(2));
It is giving correct answer for some inputs. But not for all. Can someone please explain to me the difference in this thing ?
Aman's solution impliments the "default value of the sparse matrix" by initializing the matrix to all copies of that value.
Thanks a lot for you valuable help.
Hello Rahul Gulia, You cannot make a Matrix using Zeros function because The value of each element in Sparse matrix is typically zero not Compulsorily zero, use ones function and Multiply it with the Second element of the cell. If the second element of cell is zero ultimately the matrix so generated will be zero. However if you want to go other way around it's not possible. I hope it helps
Hello Sir Walter, Looking at Aman's answer I'm in a little confusion. Is cellvec{1,2} is equivalent to cellvec{2} ?
Sir Walter, It's strange how Aman has directly focused on pin pointing elements by just providing the element number i.e. second line of for loop, instead of using {1,3}(1,1) and then {1,3}(1,2)
Following is the code I wrote, Only problem is I didn't know how to apply looping in this question. Please find that I specially mentioned the location by providing both row and column number whereas Aman has not done it in that way. He has directly used the column number as We know We will always be passing a row vector as input argument. It also makes loads of sense, there's no question about it. But for me the confusion is how come Matlab is able to understand that!
I hope you got my question Sir. I anticipate a quick reply from your side Kind Regards Arpit Srivastav
function matrix = sparse2matrix(cellvec)
a = cellvec{1,1}(1,1);
b = cellvec{1,1}(1,end);
magnitude = cellvec{1,2}(1,1);
A = magnitude*ones(a,b);
r1 = cellvec{1,3}(1,1);
c1 = cellvec{1,3}(1,2);
v1 = cellvec{1,3}(1,3);
end
r2 = cellvec{1,4}(1,1);
c2 = cellvec{1,4}(1,2);
v2 = cellvec{1,4}(1,3);
A(r1,c1) = v1;
A(r2,c2) = v2;
matrix=A;
Is cellvec{1,2} is equivalent to cellvec{2}
cellvec{2} is an example of what MATLAB calls "linear indexing". When you use a single index into an array, then MATLAB starts counting from the beginning of the array, using the order of the elements as stored in memory .
MATLAB stores elements in "column major order", https://en.wikipedia.org/wiki/Row-_and_column-major_order which means that elements in the same column are beside each other in memory. For example for A=[1 2 3; 4 5 6; 7 8 9], the order in memory is
1 %A(1,1)
4 %A(2,1)
7 %A(3,1)
2 %A(1,2)
5 %A(2,2)
8 %A(3,2)
3 %A(1,3)
6 %A(2,3)
9 %A(3,3)
The row number varies most quickly, then the column number.
It happens that if you have a vector of values, that the ordering turns out the same in memory:
Brow = [8 5 4]
8 %Brow(1,1)
5 %Brow(2,1)
4 %Brow(3,1)
Bcol = [8; 5; 4]
8 %Brow(1,1)
5 %Brow(1,2)
4 %Brow(1,3)
So if you know something is a vector, you do not need to know whether it is a row vector or column vector if you just use a single index into it.
You cannot make a Matrix using Zeros function
You can if you add the base value to the matrix of zeros()
matrix = cellvec{2} + zeros(cellvec{1});
function matrix = sparse2matrix (cellvec)
m = cellvec{1}(1,1);
n = cellvec{1}(1,2);
defult = ones(m,n) .* cellvec{1,2};
for i= 3:length(cellvec)
r1 = cellvec{i}(1,1);
c1 = cellvec{i}(1,2);
defult(r1,c1) = cellvec{i}(1,3);
end
matrix = defult;
end
function matrix = sparse2matrix(input_cell)
matrix = [];
sz = input_cell{1}; % size of matrix
default = input_cell{2}; % default value
matrix = ones(sz)*default;
for i = 3 : length(input_cell)
matrix(input_cell{i}(1), input_cell{i}(2)) = input_cell{i}(3);
end
I think this thread needs some cleanup.
DGM
DGM 2023년 11월 2일
편집: DGM 2023년 11월 2일
I weeded out some blatant duplicates and multicomment spamming. There's still about a dozen very samey answers that all vary slightly in compactness and inconsequential details like variable names, etc. That's kind of what happens when you have a simple problem. There isn't much variety. It's just a bunch of answers that are more or less the same.
Right now, I'm just sick of looking at it. You're free to pick ones you think should be pruned and flag them. I noticed you're inclined to keep things organized and am looking forward to you getting editor privileges soon.
I think this should have been soft-locked a long time ago. It's a lot easier to screen answers as they're posted than it is to try to untangle a large volume of answers that aren't necessarily in chronological order.
It is unfortunate that dev time can only be spent once. I believe the soft-lock feature has been on the list for several years now. That doesn't mean it won't come, but that does mean it might take a few more years before it arrives (following the suggestion here, I always assume I need to wait about as long as I already have waited).
@Dyuman Joshi, I you feel like it, feel free to go through this thread and flag answers for deletion. DGM (or I, or another editor) will do a double check and delete them.
Dyuman Joshi
Dyuman Joshi 2023년 11월 3일
편집: Dyuman Joshi 2023년 11월 8일
@Rik @DGM - Very well, I'll be flagging such answers as well.
And yes, I like to keep things tidy. An organized and structured thread helps someone to jump in to the discussion and engage in thread smoothly and is easy to follow for any reader as well.
Edit - @Rik, @DGM, should we manually lock the thread?
Unfortunatly the only way to do that would be to close the thread, and I personally think that would be too aggressive.
Locking threads is something that Mathworks is working on, but it isn't available yet.
It will likely take the form of requiring a minimum reputation to add to soft-locked Questions.
DGM
DGM 2023년 11월 9일
편집: DGM 2023년 11월 9일
All we can really do is post a notice clarifying that duplicates will be subject to deletion, and then manually follow up on it whenever new activity occurs. That's what I mean when I say "soft lock". Sort of like:
It helps to have the thread cleaned and cataloged enough to be able to summarize the forms that have already been exhausted. Rik's comment on that question makes it easier for the reader to grasp the scope of the whole thread, and it also makes it easier to moderate it when the rules are clearly stated and a summary is available.
Of course, that would require cleaning up and summarizing all these sprawling threads, in many of which the repeated patterns aren't as easy to distinguish. It makes it all the more time consuming when the volume of content make the page laggy. When the answers are large, it makes comparison all the more tedious. One thread I don't even want to deal with is this one:
I've tried a number of ideas to steamline the cleanup task, but if I'm honest, I don't really have a good way. After an hour or three staring at piles of slightly embellished variations with vague timestamps, I tend to start losing perspective and patience and it's hard for me to feel confident that I'm being fair.
Even without a full cleanup and formal notice, there's no reason we can't check new posts as they arrive on duplicate-prone types of threads. I tend to just keep an eye out for old threads that get bumped. It helps catch traffic on these sorts of homework//onramp/coursera threads, but it also catches the cases where people try burying their questions where they don't belong.

답변 (30개)

stanleo
stanleo 2019년 7월 7일
%simple version
function matrix = sparse2matrix(cellvec)
matrix = cellvec{2}*ones(cellvec{1});
for m=3:size(cellvec,2)
matrix(cellvec{m}(1),cellvec{m}(2))=cellvec{m}(3);
end

댓글 수: 6

Moksha Mehta
Moksha Mehta 2020년 6월 23일
편집: Moksha Mehta 2020년 6월 23일
can someone explain what is this "*ones", i looked it up and it says it creates an array of 1s. However don't we require all 0s other than the middle?
the entire code is confusing, even reading others, I'm finding it difficult despite watching the videos again.
No, the question says that the second element is a scalar specifying the default value for the sparse matrix -- so the matrix is to be all copies of that second element except where the following elements indicate it should be different.
I dont get the for statement, for m=3:size(cellvec,2). To my understanding, the size(cellvec,2) is the size of the default value for the sparse matrix, but I know that is incorrect. What does the size statement in this case do and what is m?
can someone pls explain the entire code.
There are several complete solutions on this page. What have you tried so far to piece together what every part means?
Bhoomika:
We do not have any idea what your level of experience in programming is. We would have to start from the basics of mathematics and computer science to explain the entire code in a way that we could relatively sure you would understand. That would take at least two textbooks of explanation. None of us has time to write all that.
We suggest you ask more specific questions that can be more easily answered.
AYUSH GURTU
AYUSH GURTU 2019년 5월 28일
function [matrix]=sparse2matrix(incell)
S=size(incell);
q=S(2)-2;
msize=incell{1};
mdef=incell{2};
matrix=repmat(mdef,msize);
while q>0
matrix(incell{q+2}(1), incell{q+2}(2)) = incell{q+2}(3);
q = q-1;
end

댓글 수: 4

perfect thank you sir
Kaushik Hariharan
Kaushik Hariharan 2024년 2월 13일
편집: Kaushik Hariharan 2024년 2월 13일
Could anyone explain why q=S(2)-2 = 2? I would have thought it was zero since S is size of incell which is 1*4. And s(2) could either be the 2nd index value. I am not sure how it is 4-2.
for the while loop, why are we calling q+2? and then doing q=q-1? in Incell isn;t there two values of q+2 = 4 for the first test case and if we do that we would skip the first 3-element vector. When I tried this code, I saw that it chaged 1,2 first to value of 3 then when down to 2,2 then changed the value of -3. I am trying to understand the process matlab code is taking, perhaps understanding the when s(2) = 4 would probably explain more.
There's an offset of 2 because the first two elements of the cell array store the size of the original numeric array and the fill value.
The reason that q is decremented from S(2)-2 to zero is because whoever wrote this chose to use a while loop and count backwards instead of just using a for loop.
Pavel Radko
Pavel Radko 2020년 8월 13일
편집: Pavel Radko 2020년 8월 13일
Passed all tests solution. May be not the best one (because I have no idea how to biuld default matrix in easier way), but works 100%.
% Build a matrix called "matrix" using instrictions of input "cellvec"
function matrix = sparse2matrix(cellvec)
% first we build a default matrix with size ii*jj
% we use 1st element of "cellvec" to get the size of matrix
for ii = 1:cellvec{1}(1,1)
for jj = 1:cellvec{1}(1,2)
% all elements of matrix equals to the 2nd element of "cellvec"
matrix(ii,jj) = cellvec{2};
end
end
% now we need to change elements of our default matrix
% instructions for place and value of this elements comes in "cellvec"
% from 3rd element till the end of "cellvec"
for zz = 3:length(cellvec)
% we call "matrix" elements and assign values to them from every 3rd element of subarrays of "cellvec"
matrix(cellvec{zz}(1,1),cellvec{zz}(1,2)) = cellvec{zz}(1,3);
end
end

댓글 수: 3

thanks a lot
working for the example given in the problem but others no thanks for sharing
You wrote down the complex code into very simple manner and it do work for any problem. And I understand it very well
Thanks man,
Abhishek singh
Abhishek singh 2019년 4월 24일
function [matrix]= sparse2matrix(incell);
X=size(incell);
q=X(2)-2;
msize=incell{1};
mdef=incell{2};
matrix=repmat(mdef,msize);
while q > 0
matrix(incell{q+2}(1), incell{q+2}(2)) = incell{q+2}(3);
break
end
output
matrix =
0 0 0
0 -3 0
required output
matrix =
0 3 0
0 -3 0

댓글 수: 2

# added q+1
function [matrix]= sparse2matrix(incell);
X=size(incell);
q=X(2)-2;
msize=incell{1};
mdef=incell{2};
matrix=repmat(mdef,msize);
while q > 0
matrix(incell{q+1}(1), incell{q+1}(2)) = incell{q+1}(3);
matrix(incell{q+2}(1), incell{q+2}(2)) = incell{q+2}(3);
break
end
matrix =
0 3 0
0 -3 0
but failed for
ariable solution has an incorrect value.
sparse2matrix( { [ 9 12 ], 3, [ 6 2 6 ], [ 7 1 -6 ], [ 1 10 -7 ], [ 2 2 -3 ], [ 1 4 -8 ], [ 1 11 -8 ], [ 9 11 -8 ], [ 7 8 5 ], [ 9 8 4 ], [ 9 11 7 ], [ 5 9 -4 ], [ 8 12 8 ], [ 3 6 5 ] } ) failed...
Why are you using break after one iteration of the loop ? If you are only going to do a set of instructions once, do not bother to put it in a loop.
I suggest that you read about for loops.
Jaimin Motavar
Jaimin Motavar 2019년 6월 30일
편집: Jaimin Motavar 2019년 6월 30일
can you tell me what is wrong in this answer?
function matrix = sparse2matrix(a)
e=length(a);
b=rand(a{1,1});
[m,n]=size(b);
c=a{1,3};
d=a{1,4};
for i=1:m
for j=1:n
b(i,j)=a{1,2};
end
end
for g=3:e
for f=(g-2):(e-2)
p(1,f)=a{1,g}(1,1);
end
end
for g=3:e
for f=(g-2):(e-2)
q(1,f)=a{1,g}(1,2);
end
end
for g=3:e
for f=(g-2):(e-2)
r(1,f)=a{1,g}(1,3);
end
end
for o=1:(e-2)
b(p(o),q(o))=r(o);
end
matrix=b;
end
Litesh Ghute
Litesh Ghute 2020년 3월 20일
What's wrong with my code ?
function matrix= sparse2matrix(v)
mat=zeros([v{1}(1),v{1}(2)]);
r=size(mat);
m=3;
while m <= 4
i=v{m}(1);
j=v{m}(2);
mat(v{m}(i,j))=v{m}(3);
m=m+1;
end
matrix=mat;
end

댓글 수: 1

You did not use the default value for the matrix.
ABINAND PANDIYAN
ABINAND PANDIYAN 2020년 4월 23일
편집: ABINAND PANDIYAN 2020년 4월 23일
%All the test cases are right. try this
function matrix= sparse2matrix(cellvec)
a= cellvec{1};
row=a(1);
column=a(2);
main_value= cellvec{2};
sparse_matrix= main_value * ones(row, column);
len= length(cellvec);
for i= 3:length(cellvec)
change = cellvec{i};
r=change(1);
c=change(2);
m=change(3);
sparse_matrix(r,c)=m;
end
matrix=sparse_matrix;
end

댓글 수: 4

Thanks a lot for you valuable help.
Thanks for this which made understand easily.
Bt ans coming is incorrect
thank you so much!
it is working just one suggestion for using the variable len
len = length(cellvec);
for i = i= 3:len
...
......
end
% Compteled all the test cases successfully.
function matrix = sparse2matrix(a)
cellvec = a
p= size(cellvec)
z = cellvec{1,1}
x = cellvec{1,2}
matrix = zeros(z(1,1),z(1,2));
for i=1:z(1,1)
for j= 1:z(1,2)
matrix(i,j) = x;
end
end
for j= 3: p(1,2)
y = cellvec{1,j}
matrix(y(1,1),y(1,2)) = y(1,3);
end

댓글 수: 1

can you just expalin me the for loop 1:z(1,1)
Priyansh Kushwaha
Priyansh Kushwaha 2020년 5월 16일
편집: Priyansh Kushwaha 2020년 5월 17일
function matrix=sparse2matrix(a)
b=a{1,1}
b1=b(1,1);
b2=b(1,2);
e=ones(b1,b2);
b=a{1,2}
e=b.*(e);
for i=3:length(a)
c=a{1,i};
d1=c(1,1);
d2=c(1,2);
d3=c(1,3);
e(d1,d2)=d3;
matrix=e;
end
matrix=e;
end

댓글 수: 3

matrix=e;
You overwrite matrix after the loop, so there is no point in doing that assignment inside the loop.
When there are less element in the 'a' (less than 3), So ''matrix=e'' assignment is helpful to display the output/ assigning value because in this condition(length(a)<3) the loop does not initiate.
but there is semicolon so it is not going to display anything.
utkarsh singh
utkarsh singh 2020년 5월 21일
편집: utkarsh singh 2020년 5월 21일
function matrix=sparse2matrix(a)
row=a{1,1}(1,1);
col=a{1,1}(1,2);
default=a{1,2}; % or simply default=a{1,2}
matrix=ones(row,col)*default; % matrix=ones(a{1})*deafult.....no need of finding row and col
for m=3:length(a)
matrix(a{m}(1,1),a{m}(1,2))=a{m}(1,3);
end
Taif Ahmed BIpul
Taif Ahmed BIpul 2020년 5월 24일
function matrix=sparse2matrix(cellvec)
m=ones(cellvec{1}(1),cellvec{1}(2));
m=m.*cellvec{2};
for i=3:length(cellvec)
m(cellvec{i}(1),cellvec{i}(2))=cellvec{i}(3);
end
matrix=m;
Ahmed Mamdouh
Ahmed Mamdouh 2020년 6월 7일
function matrix = sparse2matrix(ce)
matri=ones(ce{1,1}(1,1),ce{1,1}(1,2))*ce{1,2};
siz=size(ce);
i=siz(1,2);
for ii=3:i
matri( ce{1,ii}(1,1),ce{1,ii}(1,2))=ce{1,ii}(1,3);
end
matrix=matri;
Shikha Thapa
Shikha Thapa 2020년 6월 13일
function matrix=sparse2matrix(cellvec)
matrix=cellvec{1,2}*ones([cellvec{1}(1),cellvec{1}(2)]);
for a=3:length(cellvec)
matrix(cellvec{1,a}(1,1), cellvec{1,a}(1,2))=cellvec{1,a}(1,3);
end

댓글 수: 1

You can get help from the answer and code your own logic accordingly!!
Kumar Shubham
Kumar Shubham 2020년 7월 12일
편집: Kumar Shubham 2020년 7월 12일
function matrix = sparse2matrix(cellvec)
%creates matrix of req. size.
matrix=zeros(cellvec{1});
%allots assigned scalar value to all elements.
matrix(:)=deal(cellvec{2});
%used loop to maipulate matrix for result.
%use breakpoints to see approach to result step by step .
for ii = 3:length(cellvec)
matrix(cellvec{ii}(1,1),cellvec{ii}(1,2))=cellvec{ii}(1,3);
end

댓글 수: 1

Why are you using deal? Are you expecting that cellvec{2} will expand to multiple comma-separated elements? That is not going to happen with a scalar index like {2}
If you are wanting to copy the one value expectd in cellvec{2} to all elements on the left, then you do not need deal() .
Ishani Uthpala
Ishani Uthpala 2020년 8월 1일
function matrix=sparse2matrix(v)
matrix=v{1,2}*ones(v{1,1}(1,1),v{1,1}(1,2));
y=length(v);
for x=3:y
matrix(v{1,x}(1,1),v{1,x}(1,2))=v{1,x}(1,3);
x=x+1;
end
matrix;
end

댓글 수: 2

I think this will helpfull for you
What is the purpose of your line
x=x+1;
??
What is the purpose of your line
matrix;
??
function matrix=sparse2matrix(v)
rows=v{1,1}(1);columns=v{1,1}(2); %extracting total no.of rows and columns for sprase matrix
magnitude=v{2}; %extracting the default value
m=magnitude*ones(rows,columns);
for i=3:length(v) %creating the loop foor changing the values other than default
r=v{i}(1);
c=v{i}(2);
m(r,c)=v{i}(3);
end
matrix=m;
end
%this works for any no.of elements
% 100% accurate
function matrix=sparse2matrix(m)
m=cell(m);
r=m{1}(1);c=m{1}(2);dv=m{2};
ss=size(m);
matrix=sparse(r,c)+dv;
q=ss(1,2);
for ii=3:q
matrix(m{ii}(1),m{ii}(2))=m{ii}(3);
end

댓글 수: 1

Thanks, now I can cheat on my homework without having to bother understanding the problem or the solution.
On a slightly more serious note: you forgot the closing end. Although you don't need it, it has become a lot more common, especially since it is possible to put functions in script files.
Ali Raza
Ali Raza 2020년 9월 9일
function matrix = sparse2matrix(x)
M = x{1};
m = ones(M(1),M(2)) * x{2};
[~,len] = size(x);
if len == 3
i = 3;
m(x{i}(1),x{i}(2)) = x{i}(3);
else
for i = 3:len
m(x{i}(1),x{i}(2)) = x{i}(3);
end
end
matrix = m;
end

댓글 수: 1

What is your reason for treating len == 3 differently ?
Mukul Rai
Mukul Rai 2020년 10월 22일
편집: Mukul Rai 2020년 10월 22일
function matrix = sparse2matrix(a)
asize=length(a);
r = a{1}(1,1);
c = a{1}(1,2);
z = zeros(r,c)
z(:)= a{2};
if asize<=2
matrix =z
return
end
for jj=3:asize
r1 = a{jj}(1,1);
c1 = a{jj}(1,2);
n1 = a{jj}(1,3);
z(r1,c1)=n1
end
matrix =z
Abdul Quadir Khan
Abdul Quadir Khan 2020년 11월 6일
function matrix = sparse2matrix (cellvec)
m = cellvec{1}(1,1);
n = cellvec{1}(1,2);
defult = ones(m,n) .* cellvec{1,2};
for i= 3:length(cellvec)
r1 = cellvec{i}(1,1);
c1 = cellvec{i}(1,2);
defult(r1,c1) = cellvec{i}(1,3);
end
matrix = defult;
end
zehra ülgen
zehra ülgen 2020년 11월 12일
Here is another solution..
function m = sparse2matrix(a)
[t c] = size(a);
m = zeros(a{1,1});
[x y] = size(m);
for ii = 1:x;
for jj = 1:y;
m(ii,jj) = a{1,2};
end
end
for i = 3:c;
v = a(1,i);
m(v{1,1}(1,1),v{1,1}(1,2)) = v{1,1}(1,3);
end
Alberto Gil
Alberto Gil 2020년 12월 29일
편집: Alberto Gil 2020년 12월 29일
Hello people,
What do you think about this code?
function matrix=sparse2matrix(cll)
if iscell(cll)==1
% Declare values, cs=size of the array; cdn=the nominal value; N=greatest value;
cs=cll{1,1}; cdn=cll{1,2}; N=size(cll,2);
% Create the matrix with the nominal value and the size.
cm=ones(cs)*cdn;
for n=3:N;
cxdn=cll{1,n};
% Select the values of the input values.
cm(cxdn(1,1), cxdn(1,2))=cxdn(1,3);
end
matrix= cm;
else
error('The input must be a cell class');
end
end

댓글 수: 1

The question does not seem to require that you verify that the input is a cell.
xin yi leow
xin yi leow 2021년 1월 19일
function matrix=sparse2matrix(cellx)
matrix=zeros(cellx{1});
matrix(:,:)=cellx{2};
for ii=3:length(cellx)
num=cellx{ii};
matrix(num(1),num(2))=num(3);
end
end

댓글 수: 1

What does this answer add? What does it teach? Why should it not be deleted?
Minh Nguyen
Minh Nguyen 2021년 3월 27일
편집: Minh Nguyen 2021년 3월 27일
my idea about this
function matrix = sparse2matrix(ABC)
r = ABC{1}(1);
c = ABC{1}(2);
B = zeros(r,c); %make a zero matrix
B(1:end) = ABC{2}; % the sparse matrix with the second element
for i = 3:length(ABC) % and adding
a1 = ABC{i}(1,1);
a2 = ABC{i}(1,2);
B(a1,a2) = ABC{i}(1,3);
end
matrix = B;
end
Blaze Shah
Blaze Shah 2021년 9월 13일
편집: Walter Roberson 2021년 9월 13일
function matrix = sparse2matrix(cellvec)
jj = cell2mat(cellvec);
m = jj(3)*ones(jj(1,[1,2]));
n = 4;
while n<=length(jj)
m(jj(n),jj(n+1)) = jj(n+2);
n = n+3;
end
matrix = m;
Sumanth Bayya
Sumanth Bayya 2021년 10월 19일
편집: Sumanth Bayya 2021년 10월 19일
function M = sparse2matrix(cellvec)
sz = cellvec{1};
val = cellvec{2};
M = val*ones(sz);
for i = 3:length(cellvec)
el = cellvec{i};
M(el(1), el(2)) = el(3);
end
end
Ujwal Dhakal
Ujwal Dhakal 2022년 1월 7일
function matrix = sparse2matrix (cellvec)
[a b] = size(cellvec);% stores the number of cell elements in b whereas a is always 1 as cellvec is a vector
a = cellvec{1,1};% is size of the matrix that loads into a vector a
default_element=cellvec{1,2};
%preallocating the matrix to be of size m*n with all elements default
for i=1:a(1)%a(1) is the no of rows in the matrix
for j=1:a(2) %a(2) is the no of columns in the matrix
matrix(i,j)=cellvec{1,2};
end
end%matrix is generated with all elements set to default value
for ii=3:b%this loop runs from 3 to no of elements in cell vec
matrix(cellvec{1,ii}(1,1),cellvec{1,ii}(1,2))=cellvec{1,ii}(1,3);
end
昱安 朱
昱安 朱 2023년 3월 11일
function matrix=sparse2matrix(cellvec)
matrix=cellvec{2}*ones(cellvec{1,1});
for ii=3:length(cellvec)
matrix(cellvec{ii}(1),cellvec{ii}(2))=cellvec{ii}(3);
end
abdul kabeer
abdul kabeer 2023년 6월 14일
I solve it this way but dont know if this can be any shorter:
function [matrix]=sparse2matrix(v)
matrix = zeros(v{1}(1),v{1}(2))+v{2};
for i = 3:length(v)
matrix(v{i}(1),v{i}(2)) = v{i}(3);
end
Chaohua
Chaohua 2024년 7월 3일
function matrix = sparse2matrix(A)
matrix_m = ones(A{1});
matrix_m = A{2} * matrix_m;
for i = 3:length(A)
r = A{i}(1);
c = A{i}(2);
matrix_m(r,c) = A{i}(3);
end
matrix = matrix_m;

This question is locked.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2019년 4월 20일

Locked:

DGM
2024년 7월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by